Jump to content

Archived

This topic is now archived and is closed to further replies.

angel_cowgirl

Displaying the result set

Recommended Posts

Hey everyone,

 

This is probably a really simple newbie question but I've been reading my book on this stuff and looking through posts and cant find something similar.

 

The deal is I want echo the information I stored my array of the result set. I got it to work if I ask it for one field, like the name, but how can I get it to display all the fields for one item and then list the next item and its fields on a new row, etc.

 

The code ahead of what I'm going to post works, because it is just asking for the connection to the DB and I already tested that part.

 

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

//Creates a List Selection query.

$strListQuery = \"SELECT * FROM horses ORDER BY horse_name\";

//Select database

mysql_select_db($strDbName, $conMySql);

//Query the database and store the Result Set.

$rsHorseList = mysql_query($strListQuery);

//close the connection to the database

mysql_close($conMySql);

 

//Display the list of jokes returned.

//Get the number of rows in the horse table and store in $intNumRows

$intNumRows = mysql_num_rows($rsHorseList);

//for each row

for( $i = 0; $i <$intNumRows; $i++)

{

   //Get a row of data and store it in $arrHorses

   $arrHorses = mysql_fetch_array($rsHorseList);

   //display the record of each horse

   echo $arrHorses[\'\'];

   //close anchor tag

}

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

 

I know my problem is at the end with how to display it...I just dont know what else to try. I realize what I posted is an empty set and well isnt displaying anything.

 

Thank for any help!

Share this post


Link to post
Share on other sites

if you use mysql_fetch_array, the column names as well as the column offsets are available to use. the * syntax of mysql gives you all the fields, so you just reference them the same way you would a single field.

//the following two lines echo the same field
echo $arrHorses[0];
echo $arrHorses['first_field']; 

 

this is an alternative method of accessing the fields using a while loop. i'm not saying that your method is incorrect, but if you don't need the value from mysql_num_row, you can save some memories and process time.

while($arrHorses = mysql_fetch_array($rsHorseList))
{
   echo $arrHorses['first_field'];
   ......
}

Share this post


Link to post
Share on other sites

Hmm..Ok, havent tried it yet. I would have thought that since I selected all (*) that there would be a way to display it all with a word or symbol instead of having to list all the field names. I believe you thats what has to be done. I just thought that would have been the purpose of selecting all of them in the first place. Codes a funny thing!

Share this post


Link to post
Share on other sites

you can just use a foreach loop to loop through all the fields, just one extra line.

Share this post


Link to post
Share on other sites

I'm not good loops, one of my least favorite things, can you possibly give me an example of how it would work?

 

Thanks!

 

Side note: another weird thing is in my latest version of my document "\n" is not making a new line! Its only making a nonbreaking space...computers are weird!

Share this post


Link to post
Share on other sites

Also, besides "\n" not creating a new line, I am also not able to get "\t" to create a tab.

 

Anyone on any ideas how I can get tabs between the column names that I have now echoed in and then when the results are displayed a tab between each field as well? Trying to format it a lil better so it doesnt run together and so it can be read easily.

Share this post


Link to post
Share on other sites

as simple loop to echo all fields

while($arrHorses = mysql_fetch_array($rsHorseList))
{
  foreach($arrHorses as $key=>$value)
     echo $value;
  ......
}

 

\n and \t puts newline and tabs to the HTML documents (when you view source you see them), but when it's displayed on the browser, they're treated as spaces. to display newlines in HTML, use <br>. For tabs, you either have to go with   or tables. Also, you could surround the output with <pre>.

 

feel free to PM me if you need more help :)

Share this post


Link to post
Share on other sites

Ok, I tried to add some code that someone else suggested to display all the results and the table to make it look pretty but it doesnt display the results (no errors) - just no info on the page.... I also tried to put my column names in a table to make them pretty and they echo out but not in a table....I feel so stupid, I know HTML (altho I hate table code) but I guess since I'm new to PHP and all I dont know how to implement it well into the PHP...

 

help, again, please?? Thanks!!

 

P.S. I tried the code listed here too and got the same thing....no info on the page, no error just no info displayed...

 

I know I sound like a really dumb newbie..hehe

 

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]//Display column names

echo \'<table><br>\';

echo \'<b><td>Horse ID</td>\', \'<td>Horse Name</td>\', \'<td>Horse DOB</td>\', \'<td>Horse Sex</td>\', \'<td>Horse Height</td>\', \'<td>Horse Color</td>\', \'<td>Horse\\'s Sire</td>\', \'<td>Horse\\'s Dam</td></b><br>\';

echo \"</table>\";

 

//Display the list of records in the Result Set.

//Get the number of rows in the \"horses\" table and store in $intNumRows

echo \"<table>\";

$intNumRows = mysql_num_rows($rsHorseList);

for( $i = 0; $i < $intNumRows; $i++)

{

echo \"<tr>\";

$arrHorses = mysql_fetch_array($rsHorseList);

  for($j = 0; $j < 30; $j++)

  {

  echo \"<td>\".$arrHorses[j].\"</td>\";

  }

echo \"</tr>\";

}

echo \"</table>\";[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

 

Keep in mind of course theres code above and below this..but this is the section that isnt doing anything..

Share this post


Link to post
Share on other sites

$arrHorses[$j]

should have the dollar sign.

and it's totally ok to ask these questions :) we all went through these... sometimes a pro makes those mistakes. so it's no big deal :)

Share this post


Link to post
Share on other sites

YAY!!! It all looks pretty now...first I wasnt sure if id get the column headers to work too but their in the right way now and even bold :)

 

Thats so awesome, it worked for the result set part, even with the table! Thank you!! The other person had totally left off the $ for the J's and i caught it and added it in but guess i just missed one.

 

Thanks again so much for ur help! So appreciated!

Share this post


Link to post
Share on other sites

glad that it worked!! well just add some padding/margins with stylesheet and your table would look good.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.