Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting database objects into an array

Status
Not open for further replies.

biglurch

Programmer
Aug 25, 2005
35
US
Im trying to take two columns from a table and put the values into an array, so i can use the arrays to put the values into list boxes. This is what i have so far any help will be appreciated.


<code>

$ids = Array(); //array declaration

//query selecting data from table
$query = 'Select category_id FROM categories';
$result = mysql_query($query) or die(mysql_error());

//while loop continues until no more values in table
while ($row = mysql_fetch_assoc($result)) {
foreach ($line as $col_value) {
$ids($col_value);
//sets array values to each column value
}
}
echo " $ids";

</code>

Live by the code, Die by the code!!
 
why not use the PUSH function of the array ( to push each item into the array as you process it ?

Alternatively, why not just replace the array with an echo statement and print ou the rows as select OPTIONs tags where you need them ?

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005
 
After i get them outputed into the list box people will be able to choose the category then i based on the ID from that table it will pull all the entryies from another table with that id associated with it and display them all. I figured i needed the array to so when one is chosen i can reference that array object within the query to select the items from the other table.

Live by the code, Die by the code!!
 
You need to understand how web code works.

The user points his browser at your script. The browser then connects to the web server and instructs the server to run the script. The server will run the script and pipe its output to your browser. Your script will then terminate. The browser will render that HTML into something the user can manipulate. When the user submits the data from a form, the browser then invokes a script.

That second-invoked script can be the same as the first or another script entirely, but unless you're using something like a session variable (link), the data in your array will not be there. The script will have terminated and all memory will have been freed.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Now that i got it working i just wanted to post the code back so you can see.
<code>
<form method="post" action="/includes/mem_results.php" >
<?php
$query = 'SELECT * FROM categories';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<select>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\">$row[1]";

}
echo "</select>";
?>
<br />
<input type="submit" value="Submit" />
</form>
</code>

This takes the data from the database, puts the id field as the list box value and the name field as the name displayed by the list box.

Live by the code, Die by the code!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top