Forum Moderators: coopster

Message Too Old, No Replies

Building Arrays from MySQL Result

MySQL Database results - inserting into an array

         

Datalynk

4:35 pm on May 22, 2007 (gmt 0)

10+ Year Member



Hi There,

I have been sitting on this problem or some time. I have Autoboss v1.0 which is for the Autotrade.

The problem I have is that the globals.php include file currently has two arrays() defined ($cset and $mset)

These are for Catogorys and Models.

I have created additional tables in the database to hold an updatable list for both, and created additional admin pages to do this, and everything works fine.

My problem is that no matter how many different ways I try I cannot seem to create the array from a MySQL result.

It would be greatly appreciated if someone could take a look at the $cset() array example I have put into the globals.php include and see where I am going wrong.

The globals.php file is then called by index.php to do the maths and ideally should create a dynamic dropdown but doesnt...
(N.B. i commented out the original $cset array so you can see how it's put together...)

<?
// make elements for make drop down menu

mysql_select_db($database_cnx, $cnx);
$qcat = "SELECT Type FROM cat ORDER BY Type ASC";
$cat=mysql_query($qcat, $cnx);

while($cat = mysql_fetch_assoc($qcat)) //take all results
{
$cset[] = array($dbres['Type']);//No ' around variable.
}

$mset = array('Acura','Aston Martin','Audi','BMW','Citroen','Chrysler','Daewoo',
'Ford','Honda','Hummer','Hyundai','Isuzu','Jaguar','Jeep','Kia','LandRover','Lexus',
'Mazda','Mercedes-Benz','Mini','Mitsubishi','Nissan','Porsche','Saab','Subaru',
'Suzuki','Toyota','Volkswagen','Volvo');

// category elements for category drop down menu

// $cset = array('Small Hatchback','Hatchback','Saloon','Estate','Sports Performance','Commercial','MPVs','Motorbikes');

// number of vehicle to show per page
$perpage = 5;
// number of page links to show at a time
$numlinks = 10;

// thumbnail dimensions
$maxx = 80;
$maxy = 70;

?>
<?php
mysql_free_result($cat);
?>

[edited by: dreamcatcher at 5:06 pm (utc) on May 22, 2007]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]

Datalynk

4:47 pm on May 22, 2007 (gmt 0)

10+ Year Member



Sorry just to let you know...

I have got a dropdown box with 9 values (equal to the number of db records...)

But the dropdown looks like this...

Array
Array
Array
Array

etc... for each entry...

Any tips?

cameraman

4:51 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got trouble here:
$cat=mysql_query($qcat, $cnx);

while($cat = mysql_fetch_assoc($qcat)) //take all results
{
$cset[] = array($dbres['Type']);//No ' around variable.
}


You need the value of $cat from the first line - it's the resource id returned by mysql. You're overwriting it in the while and supplying the wrong parameter to mysql_fetch_assoc. Try something like this:
$cat=mysql_query($qcat, $cnx);

while($dbres = mysql_fetch_assoc($cat)) //take all results
{
$cset[] = $dbres['Type'];//No ' around variable.
}

Datalynk

5:20 pm on May 22, 2007 (gmt 0)

10+ Year Member



I see what you mean...

Ive rewritten the code as follows (hopefully a little easier to follow) in an attempt to overcome the problem, I have the correct number of results in the dropdows but no text just empty options...

and thanks for the help and prompt reply...
------------------------------------------------
mysql_select_db($database_cnx, $cnx);
$qcat = "SELECT * FROM cat ORDER BY Type ASC";
$cat = mysql_query($qcat, $cnx) or die(mysql_error());
while($row_cat = mysql_fetch_assoc($cat))
{
$cset[] = $row_cat['type'];
}

cameraman

5:35 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure you're matching the case in the fieldname - try changing $row_cat['type'] to $row_cat['Type'].

Datalynk

6:48 pm on May 22, 2007 (gmt 0)

10+ Year Member



That was it thanx...

Ure a star, and a damn fine coder!

Thanks again... D

cameraman

7:05 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aw shucks, t'weren't nothin. You almost had it, I just gave you a little nudge over the speed bump.