Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

I hate arrays!!

Links for php scripts

Moderators: macek, egami, gesf

I hate arrays!!

Postby icu90ucme » Sat Aug 02, 2003 12:44 pm

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: Select all
// 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: Select all
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: Select all
// these functions are linked together with this script
if ($url_array = get_user_urls($HTTP_SESSION_VARS['valid_username']));
  display_user_urls($url_array);
icu90ucme
New php-forum User
New php-forum User
 
Posts: 23
Joined: Mon Jul 07, 2003 2:29 pm

Postby Joel » Sat Aug 02, 2003 7:18 pm

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?
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby icu90ucme » Sat Aug 02, 2003 8:58 pm

with this set of code
Code: Select all
$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: Select all
    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
icu90ucme
New php-forum User
New php-forum User
 
Posts: 23
Joined: Mon Jul 07, 2003 2:29 pm

Postby Joel » Sat Aug 02, 2003 10:38 pm

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: Select all
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: Select all
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: Select all
while ($row = mysql_fetch_assoc($result)) {
    print $row['firstname']; //Prints first name
    print $row['lastname'];
    print $row['usrurl'];
}
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to PHP Scripts

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron