I have the following code:
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$locationListResults' AND industry = '$selected'");
while($row = mysqli_fetch_array($results))
{
echo $row['industry'];
echo $row['location'];
echo $row['title'];
echo $row['description'];
}
}
mysqli_close($con);
}
?>
Could anyone tell me how I would go about storing the echo part into a variable so I can then display it as and where I want in other parts of the site?
If I remove the echo and instead store $row as a variable when I echo that variable it only outputs once and doesn't run the loop.
You should use mysqli_fetch_all for this. The result will be an array where each element is a row and each row is an associative array with the same keys as row in your example
$data = mysqli_fetch_all($result);
$data[0]["industry"]; //Data in the first row
You can then loop over $data to output it any place on your page.
put in array,
$list = array();
while($row = mysqli_fetch_array($results))
{
$list[] = $row;
}
you may try this
$arrayList = array();
while($row = mysqli_featch_array($results)){
$arrayList[] = $row;
}
print_r($arrayList);
Put all the values in an array
$rows = array();
$x = 0;
while($row = mysqli_fetch_array($results))
{
$rows[$x]['industry'] = $row['industry'];
$rows[$x]['location'] = $row['location'];
$rows[$x]['title'] = $row['title'];
$rows[$x]['description'] = $row['description'];
$x++;
}
return $rows;
Then you can use $rows as an array.
foreach($rows as $v){
echo $v['industry']." ".$v['location']."<br />";
echo $v['title']." ".$v['description']."<br />";
}
Related
Actually with my code I print out all results together but the goal is to associate to a variable each row.
$sql = "SELECT modello FROM THING;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["modello"]; //this print all result but want to associate first result to variable $first and second to $second
}
} else {
echo "0 results";
}
Change echo $row["modello"]; to $modellos[] = $row["modello"]; like so in the example below:
$result = $conn->query("SELECT modello FROM THING");
$modellos = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$modellos[] = $row["modello"];
}
} else {
echo "0 results";
}
Now $modellos has the first in $modellos[0] and the second in $modellos[1] and the third in $modellos[2] etc etc to do with as you wish after the while loop. If you really really need them in $first and $second, add after the loop:
$first = $modellos[0];
$second = $modellos[1];
You can use array to store your results.
$result = []
while($row = $result->fetch_assoc()) {
$result[] = $row["modello"];
}
But if you really need to associate each row to a variable, you can use:
$i = 0;
while($row = $result->fetch_assoc()) {
${"result" . $i} = $row["modello"];
$i++;
}
I had a script for this that worked great in PHP 5.3. New host only supports 5.5 so I've done some modifying. So far, I can only get one row to be returned. I've been agonising for hours and I just can't see why.
Is this possibly because I'm not using array_push() after the while loop? In my original script I had it but found out with this one I got a 500 Internal Server Error with it which disappeared if I took it out.
<?php
$mysqli = mysqli_connect("localhost", "user", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$row_array = array();
$query = "SELECT title,year,author,journal,keywords,abstract,url FROM entries";
if ($result = mysqli_query($mysqli, $query)) {
/* fetch object array */
while ($row = mysqli_fetch_assoc($result)) {
$row_array['title'] = $row['title'];
$row_array['year'] = $row['year'];
$row_array['author'] = $row['author'];
$row_array['journal'] = $row['journal'];
$row_array['keywords'] = $row['keywords'];
$row_array['abstract'] = $row['abstract'];
$row_array['url'] = $row['url'];
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
What get's returned is this:
{"data":{"title":"ERGOT The Genus Claviceps","year":"1999","author":null,"journal":null,"keywords":"LSD","abstract":null,"url":null}}
Which is fine and dandy and all except there are about 9500 other rows that should be coming back with it.
You need to have a multi-dimensional array and you have taken a single dimensional array.
So, every time record comes, it overwrites older one.
Hence, you are getting the last record only.
Modify your code to:
$i=0;
$row_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
++$i;
}
$row_array= array();
while ($row = mysqli_fetch_assoc($result)) {
$tmp = array();
$tmp['title'] = $row['title'];
$tmp['year'] = $row['year'];
$tmp['author'] = $row['author'];
$tmp['journal'] = $row['journal'];
$tmp['keywords'] = $row['keywords'];
$tmp['abstract'] = $row['abstract'];
$tmp['url'] = $row['url'];
$row_array[] = $tmp;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
Your while loop keeps overwriting the same array elements in the $row_array array.
To do what you're trying to do:
while($row = mysqli_fetch_assoc($result)) {
$row_array[] = $row;
}
Your $row_array replaces every time and you get the last one. change your code to:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
$i++;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
$query = "SELECT * FROM main";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
$name= $row["name"];
$image = $row["image"];
}
}
then somewhere in my code I print it out using like echo $name; but I got only one result, which is the last row. I know I have to do foreach but what variable should I put?
foreach($result as $results) ? like this?
On every iteration, you reassign the values to $name and $image, causing it to show only the last value when it leaves the loop.
You can either echo them right away in the loop, or populate an array or an object with them, so they will be available later.
Example:
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = array('name'=>$row["name"], 'image'=>$row["image"]); // push into the array
}
var_dump($data); // it's all here now
And to echo the data later, one of the ways is foreach:
foreach($data as $row) {
echo "Name: ", $row['name'], "; Image: ", $row['image];
}
$query = "SELECT * FROM main";
$result = $db->query($query);
$row = $result->fetch_assoc();
foreach ($row as $rows) {
echo $rows['name'] ." ". $rows['image'];
}
I hava a while loop that throught to databases records. Like this:
$query = mysql_query("SELECT * FROM table");
while($articles = mysql_fetch_array($query)){
// something happen here
}
How can I echo each $articles out of the while loop ? So I don't want to have something like
while(...){
echo $articles['article_name'];
}
How can I save all the $articles['article_name'] into an array an echo them back ?
How about this:
$articleList = array();
while(...){
$articleList[] = $articles['article_name'];
}
That will put everything in the array $articleList for you.
You can do something like:
$articles = array();
while ($art = mysql_fetch_array($query)) {
$articles[] = $art;
}
print_r($articles);
$i=0; $articles = array();
$query = mysql_query("SELECT * FROM table");
while($articles[] = mysql_fetch_array($query)){
echo $articles['article_name'][$i];
$i++;
}
$query = mysql_query("SELECT * FROM table");
while($articles = mysql_fetch_array($query)){
$names[] = $articles['article_name'];
}
echo 'array of names are :';
print_r($names);
I have a query which could return multiple rows.
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
foreach ($data as $row){
echo $row['...'];
}
}
The problem is that it only prints one row, while i should have 2 rows printed.
Could someone help me out?
The problem is probably that you're re-using the $row variable. Also, the logic is a bit strange: why are you looping through $data every time you fetch a new row? Did you mean to do this instead:
while ($row = mysql_fetch_array($result)){
$data[] = $row;
}
foreach ($data as $row){
echo $row['...'];
}
Try something like:
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
foreach ($row as $k=>$v){
echo $k ." = " . $v;
}
}
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
foreach ($data as $col){
echo $col['...'];
}
echo '<br>';
}
OR
echo '<table>';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
echo '<tr>';
foreach ($data as $col){
echo '<td>';
echo $col['...'];
echo '</td>';
}
echo '</tr>';
}
echo '</table'>;
Maybe the problem is that you iterate the $data array in side the while loop, I think you want to collect all the rows in the $data, and only when while breaks to iterate it to show the results.
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
}
foreach ($data as $row){
echo $row['...'];
}
Or did I misunderstood?
Do some changes in your code for check no. of results
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
foreach ($row as $val){
echo $val;
}
}