Jump to content

Simple Multi dimension array for a mysql_fetch_array result


j.smith1981

Recommended Posts

I am having a bit of trouble with the following (just for theoretical purposes of course!).

 

Just wondered how you do the following code:

 

	$products = array(); // makes a empty array called $products

$i = 1;
$icol = 1;
while($row = mysql_fetch_array($result)) {
	$products[$i]['product1'] = $row[$icol][0];
	$i = $i++;
}

 

All its meant to do is:

 

SELECT * FROM products

 

Which the table contains just 3 columns:

productid | product  | price

1 |  product1  |  25.55

 

That is it, very simple, but wanted to make a multi dimension array that will be 3 columns wide, then however many rows (hence the while loop and array fetch etc).

 

Any help is appreciated, just coming from a VBA background when it comes to the arrays in VB lol.

 

Thanks for any help in advance,

Jeremy.

Link to comment
Share on other sites

Do not know whats going wrong with my brain today (not enough coffee I would have thought lol).

 

Of course this is it:

 

	while($row = mysql_fetch_array($result)) {
	$products[$i]['productid'] = $row[0];
	$products[$i]['product'] = $row[1];
	$products[$i]['price'] = $row[2];
	$i = $i++;
	print_r($products);
}

 

Is there out of pure interest any better way of doing this perhaps though? (just to not waste your time hopefully).

 

I do apologise about my first post though lol, I am a douchebag sometimes lol.

 

Thanks and I look forward to any replies,

Jez.

Link to comment
Share on other sites

i'm having a bit of trouble trying to understand exactly what you are doing or why, what exactly are you planning to do with the $products array?

 

when you loop through a mysql result like that, you can access the fields of each row like: $row['productid']

Link to comment
Share on other sites

Ah of course, I mean but that would be the function mysql_fetch_assoc (assoc meaning associative array).

 

I believe anyways, I mean if you use _array being the last bit of that function then it would mean using integers to represent the columns, am I thinking through this properly?

 

Sorry its just to see how to fill up a multi dimension array just out of interest, want to try and write an ecommerce website, but before I do that I wanted to work out somethings I have had great trouble understanding in the past, finally getting somewhere with it though.

 

Thanks ever so much,

Jez.

Link to comment
Share on other sites

Not exactly.

mysql_fetch_row - returns an enumerated array

mysql_fetch_assoc - returns an associative array

mysql_fetch_array - returns either enumerated or associative array, or both depending on the modifier MYSQL_NUM, MYSQL_ASSOC, or MYSQL_BOTH. Defaults to MYSQL_BOTH if no modifier is specified.

Link to comment
Share on other sites

Much shorter and easier:

 

while($row = mysql_fetch_assoc($result)) {
   $products[] = $row;
}
print_r($products);

 

That would only be a single dimension surely?

 

It is infact a multi dimension array I am after, if its only got one [] square bracket then its instantly a single dimension array, if its got 2 [][] or more then its a whatever number but a multi dimension array.

Link to comment
Share on other sites

Sorry I dont think I made this clear enough.

 

It's ok I went onto explain more, you probably missed it lol (I do that all the time its cool).

 

But this issue has now been resolved it did when I posted my explanation, I just was an idiot not to realise what I was trying to do lol.

 

Thanks for your help though really appreciate the effort!

 

Thanks again,

Jez.

 

PS The post above is what I did:

 

Do not know whats going wrong with my brain today (not enough coffee I would have thought lol).

 

Of course this is it:

 

	while($row = mysql_fetch_array($result)) {
	$products[$i]['productid'] = $row[0];
	$products[$i]['product'] = $row[1];
	$products[$i]['price'] = $row[2];
	$i = $i++;
	print_r($products);
}

 

Is there out of pure interest any better way of doing this perhaps though? (just to not waste your time hopefully).

 

I do apologise about my first post though lol, I am a douchebag sometimes lol.

 

Thanks and I look forward to any replies,

Jez.

Link to comment
Share on other sites

Did you try it and then see what the print_r() looks like?

Sorry I dont think I made this clear enough.

 

So in other words, no, you didn't try it? Perhaps you should have because this it what it returns with data in a test table I set up.

 

Query:

$query = "SELECT product_id, product, price FROM test_table";
$result = mysqli_query( $dbc, $query ) or die(mysqli_error($dbc));
while( $row = mysqli_fetch_assoc($result) ) {
   $products[] = $row;
}

echo '<pre>';
print_r($products);
echo '</pre>';

 

Returns:

 

Array

(

    [0] => Array

        (

            [product_id] => 1

            [product] => Boots

            [price] => 9.99

        )

 

    [1] => Array

        (

            [product_id] => 2

            [product] => Hat

            [price] => 7.99

        )

 

    [2] => Array

        (

            [product_id] => 3

            [product] => Coat

            [price] => 19.50

        )

 

    [3] => Array

        (

            [product_id] => 4

            [product] => Gloves

            [price] => 3.00

        )

 

    [4] => Array

        (

            [product_id] => 5

            [product] => Skis

            [price] => 199.99

        )

 

    [5] => Array

        (

            [product_id] => 6

            [product] => Goggles

            [price] => 10.00

        )

 

)

 

 

Then if you wanted the main $products[] array index numbers to be the same as the product_id they represent, all you'd have to do would be this:

$products[$row['product_id']] = $row;

Link to comment
Share on other sites

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.