Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a page that will display the names of people from one data base that is joined with another that tracks when two names are linked. Every thing works fine. However; I cannot make the data or specifically the right user name ($id2u) and a string ($permissions) transfer from the input boxes when the user selects one. The onclick event should then show the data fields as per the permission string. The problem is the displayed info is always the last user in the DB that was ran through the loop. Am trying to get the info to carry forward on the onclick event when a check box is clicked so that their info is displayed.

This is he function to call the display of data.

function display1(type) {document.getElementById("pi").style.display = "none"
 document.getElementById(type).style.display = ""}

This is the call to the DB that generated the input boxes for each person.

$result1  = mysql_query('SELECT * FROM level1 LEFT JOIN linkreq ON level1.idnumber = linkreq.idme ORDER BY LEAST(lname, fname) DESC');
for ($i = mysql_num_rows($result1) - 1; $i >= 0; $i--){
    if (!mysql_data_seek($result1, $i)){echo "Cannot seek to row $i: " . mysql_error() . "\n";continue;}
    if (!($row = mysql_fetch_assoc($result1))) {continue;}
    if($_SESSION["idnumber"] == $row['id2u']){
    if($row['returnack'] == 0){$id2u = $row['idme']; echo'<font color="#FF6600">*</font>&nbsp;&nbsp;&nbsp;'.$row['title'].' '.$row['fname'].' '.$row['mname'].' '.$row['lname'].' '.$row['suffex'].'<br />';}
    else{
    $permissions  = $row['permissions'];
    parse_str($permissions);
    $permissions; 
    $id2u = $row['idme'].'&nbsp; ';
    echo '<input type="checkbox"  value = " .$id2u. " onClick="display1(\'pi\');">&nbsp;&nbsp;&nbsp;'
    .$row['title'].' '.$row['fname'].' '.$row['mname'].' '.$row['lname'].' '.$row['suffex'].'
    <br />';}}}

This is the data that gets displayed onclick(pi) but displays last user not the selected one. Basically if the $id2u variable data would carry over from the check box this would work. As can manually force it by setting $id2u = xxxxxx; just before the query.

$result2  = mysql_query('SELECT * FROM pi WHERE idnumber = "'.$id2u.'"');


for ($i = mysql_num_rows($result2) - 1; $i >= 0; $i--){
    if (!mysql_data_seek($result2, $i)){echo "Cannot seek to row $i: " . mysql_error() . "\n";continue;}
    if (!($row = mysql_fetch_assoc($result2))) {continue;}
    echo '<br>row #'.$row['idnumber'];
    if($id2u == $row['idnumber']){echo'...// PRINTS OUT DATA //...}}

</span>

I know this should be in mySQLi but for now just need to get it to work will convert later.

share|improve this question
Thanks @jOK for editing my question. Was not sure how to put the code in the boxes? IBNOOB. – Hnioxoe Feb 13 at 18:23

1 Answer

Why don't you use a while() loop ?

$query = mysql_query("...");
while($row = mysql_fetch_array($query))
{
   ...
}

This always works fine for me :)

share|improve this answer
Need a little more info as not sure what the difference in outcome of a for loop or a while loop will be. The problem is not in running the loop. After the loop runs the $id2u variable is not who the person should be compared to who was checked in the checkbox – Hnioxoe Feb 13 at 20:43
Couldn't you use a query that joins the databases and shows a list of all the people linked to the current one ? – Vincent Kelleher Feb 14 at 8:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.