0

my database table is named order. it has a row like order. I store values like this 1,2,3,4,5. Now i would like to add to array and from it out info..

I tried to do that but it is not working...

here is my code:

$sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
$row = mysql_fetch_assoc($sql);
$sql_order = $row['order'];

$array = array($sql_order);

foreach($array as $x) {
    $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    echo $row['product_name'].'<br />';
}

if want check print_r($array) Output

Array ( [0] => 1,23,4,5 )

this one is not working.. i think its supposed to be like this: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

3
  • Normalize your database, then you also won't run into such problems! Commented Aug 2, 2015 at 18:07
  • @Rizier123 i want to out $array like this Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) help me for this plzz... Commented Aug 2, 2015 at 18:09
  • 2
    If you normalize your database you won't even have this problem! So normalize your database and don't store multiple data in one field! Commented Aug 2, 2015 at 18:10

2 Answers 2

2

FASTEST APPROACH

You need to use explode() function. Here is an example of it :

<?php
    $array = array('0' =>'1,2,3,4,5');
    $array = explode(',', $array[0]);
    var_dump($array);
?>

Here is your updated code, to get array in that format.

$sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
$row = mysql_fetch_assoc($sql);
$sql_order = $row['order'];

$array = array($sql_order);
$array = explode(',', $array[0]);

foreach($array as $x) {
    $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    echo $row['product_name'].'<br />';
}

Note this is solution which you are looking for. But it isn't recommended due to reason told to you as comment.


PROPER WAY TO HANDLE THIS

You should ideally normalize your Database so this kind of problem don't come even in future to you.
Here is a proposed table structure change, which you can consider, depending on your need & time.

  • Remove order column from your table. Add a new table named order_suborders as follows:

|   COLUMN   |    TYPE     | 
|:-----------|------------:|
|parent_order|      int    |     
| order_id   |      int    |

You can change name of columns and table according to your wish.

  • Move old data accordingly.
  • Use query SELECT order_suborders.order_id FROM order, order_suborders WHERE order.id = ".$_GET['id']." AND order.id = order_suborders.parent_order
Sign up to request clarification or add additional context in comments.

4 Comments

I think this is really misleading, since OP's biggest problem is, that his database is not normalized. And with this he won't solve the problem at the root, he will run into much more and much worse problems if he doesn't normalize it.
Yes, I know that he hasn't normalized it, added that point too. But this solution is what he is looking for (though not recommended).
Probably because he don't know how a normalized DB looks like, that's why OP is asking for this.
Hmm, maybe, ok wait, adding normalization approach too. Though we can't assume ourselves that he need help in normalizing his table, still it might be useful for him. :)
0

you can use explod to split with ","

  $sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    //$array = array($sql_order);
    $array=explod(",",$sql_order);

    foreach($array as $x) {
        $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
        $row = mysql_fetch_assoc($sql);
        $sql_order = $row['order'];

        echo $row['product_name'].'<br />';
    }

2 Comments

OP wouldn't even have this problem if his database is normalized. And with this you don't solve OP's real problem.
yes, i totally agree with what you say. he need to normalize his database. it's my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.