I hate arrays!!

A

Anonymous

Guest
i'm having trouble showing all the information that im pulling from mysql database with this array. this is a lot of code but i hope you can follow it. I want to show First Name and Last Name, and URL, but the array only shows the FIRST name and not both

Code:
// this is the first function
function get_user_urls($username)
{
  //extract from the database all the URLs this user has stored
  if (!($conn = dbconnect()))
    return false;
  $result = mysql_query( "select usrfname, usrlname, usrurls 
                          from usrinfo
                          where username = '$username'");
  if (!$result)
    return false; 

  //create an array of the URLs 
  $url_array = array();
  for ($count = 1; $row = mysql_fetch_row ($result); ++$count) 
  {
    $url_array[$count] = addslashes($row[0]);
  }  
  return $url_array;
}
Code:
function display_user_urls($url_array)
{
  //display the table of URLs

?>
  <br />
  <form name=bm_table action="delete_bms.php" method=post>
  <table width=300 cellpadding=2 cellspacing=0>
  <?php
  $color = "#cccccc";
  echo "<tr bgcolor=$color><td><strong>Bookmark</strong></td>";
  echo "<td><strong>Delete?</strong></td></tr>";
  if (is_array($url_array) && count($url_array)>0)
  {
    foreach ($url_array as $url)
    {
      if ($color == "#cccccc")
        $color = "#ffffff";
      else
        $color = "#cccccc";
      echo "<tr bgcolor=$color><td><a href=\"$url\">".htmlspecialchars($url)."</a></td>";

// ------ the $url only displays FIRST NAME

      echo "<td><input type=checkbox name=\"del_me[]\"
             value=\"$url\"></td>";
      echo "</tr>"; 
    }
  }
  else
    echo "<tr><td>No bookmarks on record</td></tr>";
?>
  </table> 
  </form>
<?php
}
Code:
// these functions are linked together with this script
if ($url_array = get_user_urls($HTTP_SESSION_VARS['valid_username']));
  display_user_urls($url_array);
 
I'm not even sure what you want to do.

Do you want to get a row from the database. And for all the first name, last name and url you want printed out in rows? Or are you getting many rows?
 
with this set of code
Code:
$result = mysql_query( "select usrfname, usrlname, usrurls 
                          from usrinfo 
                          where username = '$username'");
i fetch usrfname, usrlname, and usrurls from my table..

when i try to print them out from the array using this code
Code:
    foreach ($url_array as $url) 
    { 
      if ($color == "#cccccc") 
        $color = "#ffffff"; 
      else 
        $color = "#cccccc"; 
      echo "<tr bgcolor=$color><td><a href=\"$url\">".htmlspecialchars($url)."</a></td>";
the only thing that $url equals is usrfname, i want $url to return to me usrfname, usrlname, and usrurls
 
You're problem is not in the printing, but in the selecting. I don't recommend the way you've done the whole thing.
for ($count = 1; $row = mysql_fetch_row ($result); ++$count)
{
$url_array[$count] = addslashes($row[0]);
}
In this code you are basically saying "use column 1 of the database and store it in the array. Column 1 is first, if you were using mysql_fetch_array or mysql_fetch_assoc then you would've had $url_array[$count] = addslashes($row['firstname']). If you want to have a full result array then I think this is what you should do.

Code:
while ($row = mysql_fetch_assoc($result)) {
        $url_array[] = $row;
}
This will mean you have things like $url_array[0]['firstname'] or in your foreach you could have this in your printing code.

Code:
foreach ($url_array as $row) {
    print $row['firstname']; //Prints first name
    print $row['lastname'];
    print $row['usrurl'];
}

Do you get the picture? Alternatively you could combine you're selecting and display functions and use this code, but of course something how you want.

Code:
while ($row = mysql_fetch_assoc($result)) {
    print $row['firstname']; //Prints first name
    print $row['lastname'];
    print $row['usrurl'];
}
 
Back
Top