Page 1 of 1

how to know which item of while loop that are in <a> is sele

Posted: Wed Jan 11, 2012 7:02 am
by prasanna
<?php
session_start();
$database="myfriend";
$userid=$_SESSION['user'];
mysql_connect("127.0.0.1","root","");
$db_found=mysql_select_db($database);
$sql="select fuserid from friends where userid='$userid';";
$result=mysql_query($sql);
$id=0;
$users=array();
?>
<body >

<center>
<p> your friends list:</p>

<?php
while($db_field=mysql_fetch_assoc($result))
{
?>

<a href="displayprofile.php" id='$id' onclick="validate(this)"><?php print $db_field['fuserid']."<br><br>";?></a>
<?php
$users[$id]=$db_field['fuserid'];
$id=$id+1;
}?>

<head script type="javascript/text">
<script>
function validate()
{
var id1=document.getElementById(</script><?php print $id ?><script>);
</script>

<?php
$_SESSION['id'] ?><script >=id1;
}
</script>

<?php
$id1=$_SESSION['id'];
$_SESSION['fuserid']=$users[$id1];
?>




thw o/p:name1
name2
..
depending on the selected name my code will be changing .Then how to know which item of while loop is selected at run time.plzz some one help this code is used in my project that displays profile of particular user depending on name.

Re: how to know which item of while loop that are in <a> is

Posted: Wed Jan 11, 2012 7:42 am
by twinedev
Can you edit your original post and wrap code with [ syntax = php ] and [ /syntax ] (and remove the spaces, had to add them so it display that instead of interpreting that).

To be honest, your code was too much of mess to figure out what you wanted.... Using HTML tags that don't exist, apparently trying to mix and match Javascript and PHP I am guessing?

If your thought was to execute some PHP during the call to validate(), well it won't happen like that. You need ot do some AJAX calls back to the server to execute things. Remember, PHP is executed on the server, and all of its output is sent to the browser, which then the browser executes the javascript. (the PHP script is already done running).

Try starting with a little cleaner code such as the following. Take it and add in comments to what you are wanting to happen then we might be able to help you better.
(Note, only use one of the two $SQL = ".... lines. If the ID is set to be integer, use the second, otherwise use the first)

Code: Select all

<?php
    session_start();

    $database="myfriend";
    $userid=$_SESSION['user'];

    $dbConn = mysql_connect("127.0.0.1","root","")
        or die('ERROR: Could not connect to database server');

    mysql_select_db($database,$dbConn)
        or die ('ERROR: Could not use database specified');

    // If 'userid' is a string:
    $SQL = 'SELECT `fuserid` FROM `friends` WHERE `userid`="'.mysql_real_escape_string($userid).'"';

    // If 'userid is an integer:
    $SQL = 'SELECT `fuserid` FROM `friends` WHERE `userid`='.(int)$userid;

    $users = array();
    $rsFirends = mysql_query($SQL) or die ('ERROR: Could not run query: '.mysql_error());

    if ($rsFirends && mysql_num_rows($rsFirends)>0) {
        while ($aryTemp = mysql_fetch_assoc($rsFirends)) {
            $users[] = $aryTemp['fuserid'];
        }
        mysql_free_result($rsFirends);
    }
    unset($rsFirends);

    $id = 0;

?><html>
<head>
    <title>My Site</title>
    <script type="text/javascript">
        function validate(id1) {
            // id1 will be the <a href element clicked on...

            // Couldn't quite figure what else you were wanting to do...
    }
    </script>
</head>
<body>
    <div style="width: 100%; text-align: center;">
        <p>Your friends list:</p>

        <?php foreach($users as $friendID): ?>

            <a href="displayprofile.php" id="<?php echo $id++;?>" onclick="validate(this)">
                <?php echo htmlspecialchars($friendID,ENT_QUOTES); ?><br><br>
            </a>

        <?php endforeach; ?>

    </div>
</body>
</html>

Re: how to know which item of while loop that are in <a> is

Posted: Thu Jan 12, 2012 12:55 am
by prasanna
in function validate i am trying to put the selected id in php session variable because by clicking on the link i need to know which id is selectecd and that id is to be sent to displayprofile so plzz help me by giving the code what to be in validate function..

Re: how to know which item of while loop that are in <a> is

Posted: Thu Jan 12, 2012 2:58 am
by twinedev
Ok, if all you are needing is that displayprofile.php has the id, then you don't even need to mess with $_SESSION, you can just pass it via the query string.

Gist of it here...

Program the page to go to the links as such:

If ProfileID is numeric:

Code: Select all

<a href="displayprofile.php?profileid=<?php echo (int)$friendID; ?>">
If ProfileID is text:

Code: Select all

<a href="displayprofile.php?profileid=<?php echo urlencode($friendID); ?>">
Now, inside displayprofile.php, the following variable will contain that friendID they clicked on: $_GET['profileid'] (note, this, along with $_POST, $_COOKIE and some $_SERVER variables can be changed by the visitor, so never use them directory or trust them to be what you expect. The code below will use them safely.)

Code: Select all

<?php
// If profile id is numeric:
$intProfileID = (isset($_GET['profileid'])) ? (int)$_GET['profileid'] : 0;
if ($intProfileID >0) {
    // At this point, $intProfileID contains ONLY a number and is safe to use for things like SQL statements.
    
    // Look it up in database
    // Do what you need to do
}
else {
   echo "Invalid User Id";
}

// ----------------------------------------------------
// if profile id is a string (ie.twinedev)
$strProfileID = (isset($_GET['profileid'])) ? $_GET['profileid'] : '';
if ($strProfileID != '') {
    // Note, this can still contain things to cause issues, so use as:
    $SQL = 'SELECT * FROM `tblProfiles` WHERE `ProfileID` = "'.mysql_real_escape_string($strProfileID).'"';

    // Look it up in database
    // Do what you need to do , if you need to echo out $strProfileID, be sure to do it like this:
    echo 'Showing profile for: ',htmlspecialchars($strProfileID,ENT_QUOTES);
}
else {
   echo "Invalid User Id";
}
To help give you the better code, in your database, is fuserid an integer (number) or a string (text) value? Also, how protected do you need profiles? Should I only be able to see specific ones that are my friends? (note, not talking about the code, i'm talking in general of browsing your site, if I shouldn't see others, I'll give you code to help deter that)

-Greg

PS. any of the functions you do not understand, remember you can always just browse to http://www.php.net/[i]function_name[/i] to get good docs on what they do, what they expect to be passed, what they return, and usually a few good examples.

Re: how to know which item of while loop that are in <a> is

Posted: Thu Jan 12, 2012 5:38 am
by prasanna
thaq for your help this is working and i learnt a new concept.