0

Hi Everyone, I have the following piece of code & I am trying to use to create an array of "Playing Cards" from a mysql db.

I am just not sure how to one single complete array !
Thanks for looking and replying with your suggestions

2
Contributors
4
Replies
21
Views
4 Years
Discussion Span
Last Post by DaveyMoyes
0

here is the php for the above question

            $sth = mysql_query("SELECT * FROM cards2 where suit='clubs'");
            $rows = array();
            $i=0;
            while($r = mysql_fetch_assoc($sth)) {
                $i++;
                array_push($rows, $r["suit"]);
                array_push($rows, $r["card"]);

                foreach($rows as $key => $value)
            {
            }
                print_r($rows);

the array is displaying, but it is looping through from the start again for each card
Array ( [0] => clubs ) Array ( [0] => clubs [1] => clubs ) Array ( [0] => clubs [1] => clubs [2] => clubs )

Edited by DaveyMoyes: forgot to add array

0

You should close the while loop, then do the foreach. The foreach is inside the while loop, causing this effect.

0

Hi thanks for replying -

But for some reason, I am getting each result 6 times,
the array is printing out like this
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8

When it should only be printing 1 clubs 8

Can anyone help point me in the right direction why this is happening
here is the code I am using -

1                   ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    $sth = mysql_query("SELECT id, suit, card FROM cards2 where id='1' or id='2' or id='3' or id='4' ORDER BY RAND() LIMIT 3");
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // result
                    $clubresult = mysql_query($query) or die ("no query");                  
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // array
                    $rows = array();

                    while($rows = mysql_fetch_array($sth)) {

                        //foreach($rows as $key => $value)
                        foreach($rows as $key => $value){ 

                        echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";
                            }

Thanks

1

Hi, Sorry... But this is now fixed, I was echoing out in the wrong place -

The fix, if anyone is reading this is below -

                            }
                        echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";

                        }

Here is the problem code I was using

                        echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";
                            }
                        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.