ecabrera Posted August 9, 2012 Share Posted August 9, 2012 How would i make the test variable an array so once it's an array i can do a foreach loop so i can use suffle <?php require "admin/scripts/connect.php"; $newquery = mysql_query("SELECT * FROM test WHERE live=1"); do{ $test = stripslashes($row['message']); $f = stripslashes($row['firstname']); $l = strtoupper(stripslashes($row['lastname'])); echo "<p align='justify'>$test</p>"; echo "<h4 align='right'>$f.$l</h4>"; }while($row = mysql_fetch_assoc($newquery)); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/ Share on other sites More sharing options...
MMDE Posted August 9, 2012 Share Posted August 9, 2012 while($row = mysql_fetch_assoc($newquery)){ // you are looping through it now, and can access the data as if $row was an array with the various field names as keys. } http://php.net/manual/en/function.mysql-fetch-assoc.php You can decide to put it all organized into some array when you are looping through the results row by row. Maybe there is some id you get from the result that you want to identify the data by. $array = array(); while($row = mysql_fetch_assoc($newquery)){ $array[$row['id']] = $row; } foreach($array AS $rows=>$key){ // whatever } sorry if there are any typos... (haven't slept in over a day ) Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368051 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 Use a while() loop and not do...while() and $test[] = $row['message']; Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368053 Share on other sites More sharing options...
ecabrera Posted August 9, 2012 Author Share Posted August 9, 2012 Thanks for helping me im on the 24 coding so tried but have to finish so what i want to do is make a testimonial box that will display each testimonial separate but in order to do that i will have to make it a array $test[] = stripslashes($row['message']); $f = stripslashes($row['firstname']); $l = strtoupper(stripslashes($row['lastname'])); foreach($test AS $messages){ echo "<p align='justify'>$messages</p>"; } echo "<h4 align='right'>$f.$l</h4>"; } but some how its showing duplicate's i dont understand Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368055 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 put the foreach() loop AFTER the while loop Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368057 Share on other sites More sharing options...
ecabrera Posted August 9, 2012 Author Share Posted August 9, 2012 ok so that work but one problem that i dont understand is can the message from that user how would i get there fristname also i tried this but nothing foreach($test AS $messages){ echo "<p align='justify'>$messages</p>"; foreach($f as $first) { echo "<h4 align='right'>$first</h4>"; } } unless this might work foreach($test AS $messages){ echo "<p align='justify'>$messages</p>"; foreach($message as $first) { echo "<h4 align='right'>$first</h4>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368058 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 You would need to store the message and firstname in the array. Inside the while loop put $test[] = array ( 'message' => $row['message'], 'first' => $row['first'] ); Then foreach ($test as $data) { echo $data['first'] . '<br>' . $data['message'] .'<br><br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.