简体   繁体   中英

how do I look an array to extract data

Hi all Im trying to query a database and store the results ($searchResults[]) as an array :

<?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'");

        $searchResults = array();

        while($row = mysqli_fetch_array($results))
            {
                $searchResults[] = $row['industry'];
                $searchResults[] = $row['location'];
                $searchResults[] = $row['title'];
                $searchResults[] = $row['description'];
            }

    }
                mysqli_close($con);
}

?>

the problem im getting is when I try to echo the result:

<?php 

echo $searchResults[0];

?>

its only bringing back 1 result not displaying all the results in the arrray as i want it to.

Could anybody please point out what it is im doing wrong.

Any help would be greatly appreciated

Do like this

<?php 
print_r($searchResults); // Prints all array elements
?>

Alternatively, you can make use of a for loop to echo all elements too..

foreach($searchResults as $k=>$v)
{
 echo $v;
 echo "<br>";
}

Your code puts your data into 1D array. You probably want sth else so instead of this:

$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];

do this:

$tmp = array();

$tmp['industry'] = $row['industry'];
$tmp['location'] = $row['location'];
$tmp['title'] = $row['title'];
$tmp['description'] = $row['description'];

$searchResults[] = $tmp;

or just this (thanks to Barmar):

$searchResults[] = $row;

This way you store your data as 2D array. So every row you obtain remains in one subarray.

To print the row (which is now in 2D array) iterate over subarrayw:

foreach($one_of_searchResult_rows as $k => $v)
{
    // do whatever you need with $k and $v
}

Lets make it simple :

$serach_result=mysqli_fetch_all ($result,MYSQLI_NUM);
//you should use print_r while trying to print an array
print_r($search_result[0]);

Reference : mysqli_fetch_all

I think you want a 2d array. Try this code snippet :

$searchResults = array();

    while($row = mysqli_fetch_array($results))
        {
            array_push($searchResults,$row);
        }

This should push each row as an associative array in every cell of your final searchResuts array. You could then access the values like:

echo $searchResults[0]['industry']; //and so on
echo $searchResults[0]; //Should print out the first match/row of the sql result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM