Jump to content

Variable and array


ecabrera

Recommended Posts

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/266855-variable-and-array/
Share on other sites

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 :P)

Link to comment
https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368051
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368055
Share on other sites

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>";
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368058
Share on other sites

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/266855-variable-and-array/#findComment-1368062
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.