Page 1 of 1

[SOLVED] from db into array

Posted: Tue Dec 13, 2005 4:25 am
by rsmarsha
I want to select a set of id's from a db with a query, which i have. Once selected i need them put into an array which can then be used to loop through. I can create an array to loop through myself, but can't seem to create one using a result set from the db. Any ideas?

Posted: Tue Dec 13, 2005 7:48 am
by Charles256
$array=array();

then....

Code: Select all

$i=0;
while ($test=mysql_fetch_object($myqueryresult))
{
 $array[i]=$test->id;
$i++;
}
and that should make your populated array....then to loop through it immediatly afterwards

Code: Select all

for($L=0;$L<$i;$L++)
{
 echo $array[L];
}
tada?:-D

Posted: Wed Dec 14, 2005 3:44 am
by rsmarsha
Got this so far.

Code: Select all

if ($_POST['type']=='pc')
{
$set = "SELECT cat_id FROM categories WHERE cat_type!='lp'";
$sq = mysql_query($set) or die("Query $set Failed".mysql_error());
}
else
{
$set = "SELECT cat_id FROM categories WHERE cat_type!='pc'";
$sq = mysql_query($set) or die("Query $set Failed".mysql_error());
}
$array=array();
$i=0; 
while ($type_row=mysql_fetch_object($sq)) 
{ 
$array[i]=$type_row->cat_id; 
$i++; 
}
for($L=0;$L<$i;$L++) 
{ 
echo 'test: '.$array[L].''; 
}
Doesn't seem to echo the id's.

Posted: Wed Dec 14, 2005 3:56 am
by JayBird
this line...

Code: Select all

$array[i]=$type_row->cat_id;
...should be...

Code: Select all

$array[$i]=$type_row->cat_id;
and this line...

Code: Select all

echo 'test: '.$array[L].'';
...should be...

Code: Select all

echo 'test: '.$array[$L].'';

Posted: Wed Dec 14, 2005 4:03 am
by rsmarsha
Thanks :) Seems to be working. :)

Posted: Wed Dec 14, 2005 5:31 am
by Charles256
sorry.that's my c++ coming through... :oops: