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

Recommended Answers

All 4 Replies

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 )

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

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

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 />";
                            }
                        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.