Jump to content

Recommended Posts

Hello everyone.  I have what I think is a simple issue, but it's turned my mind to mush!

 

Here's the situation.  I have a query that pulls 4 records from a mysql db.  Let's say that each record is the name of a fruit:  Orange, Lemon, Lime, Grape.  The fruits are in that order in the db.  Record 1 is "Orange", Record 2 is "Lemon" etc.  My table has 2 fields - FruitID and FruitName, and 4 records.

 

I have the query working with a "while" statement so the list of fruit prints to the screen just fine.  Of course, the list prints as reflected in the db.  Orange is first, followed by Lemon, then Lime etc.

 

What I'd like to do is reorder the fruit for printing. 

 

Or, to put it another way, what if I wanted to print each Fruit name just once, but in different places in a web page, not as a list? 

 

How do I do this?  I assume I have to use some sort of array and assign each fruit to a variable that can be echoed to the screen.

 

Here's my working code for reference:

 

$Link = mysql_connect("$Host", "$User", "$Password") or die ('I cannot connect to the database.' . mysql_error());

mysql_select_db("$DBName");

$fruitquery = mysql_query("SELECT * FROM fruit");

 

while($Row = mysql_fetch_array($fruitquery)) {

print ("$Row[FruitName]");

 

}

 

Any suggestions?  I know this can't be difficult but my brain just isn't grasping the answer.

 

Thanks

dlc

 

 

 

 

<?php

$Link = mysql_connect("$Host", "$User", "$Password") or die ('I cannot connect to the database.' . mysql_error());
mysql_select_db("$DBName");
$fruitquery = mysql_query("SELECT * FROM fruit");

while($Row = mysql_fetch_array($fruitquery)) {
       $fruitNames[$Row['fruitid']] = $Row['FruitName'];
}

print $fruitNames[0];  // should print Orange

sort($fruitNames);

print $fruitNames[0];  // should print Grape

?>

 

That is how you get it in an array and you can sort the array to display particular values.

 

 

--FrosT

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.