-1

I have following code, I try to style output with CSS, but I have small problem, my code show all database entries, which is OK, but when I remove comment from WHILE loop, and comment echo, its showing only first row of entries from database.how can I do same thing and show multiple results from database by use variables in While Loop?:

<?php

error_reporting(0);
require 'connect.php';
$search = $_POST['search'];
//$checkout = $_POST['checkout'];

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM area where destination='{$search}'";
$result = mysqli_query($conn, $sql);

if($count = $result->num_rows) {
    echo '<p>', $count, '</p><br><br>';
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        
        echo $row['destination'],' ',$row['place'], $row['tosee'], '<br>';
         
    /$destination =$row["destination"]; 
    //$place =$row["place"]; 
    /$destination =$row["destination"]; 
    //$place =$row["place"]; 
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

and inside my HTML file:

location:  <?php echo $destination; ?>
places:  <?php echo $place ; ?>
views:  <?php echo $tosee; ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
andrewpuni
  • 33
  • 5
  • That's expected behaviour. Your while loop loops through the result set updating the variables as it goes. That all happens before it gets to your `html` file which [presumably] comes later in your code. So when it does get to your `html` file the _only_ last looped set of variables is output – Steven Apr 10 '21 at 18:51
  • You'd need to put the row data into an array, then later you'd need PHP to loop over the array and output one set of html per row. – ADyson Apr 10 '21 at 19:06
  • And how can i do that? i dont know PHP well,just started,, – andrewpuni Apr 10 '21 at 19:10
  • 1
    Presumably the `echo` in your while shows the data in the correct place on your page? Just simply change that `echo` to the format you want the data `echo "Location: {$row["destination"]}
    Places: {$row["place"]}
    Views: {$row["tosee"]}";`
    – Steven Apr 10 '21 at 19:14

2 Answers2

1

Do this...

$out .= $row['destination'].' '.$row['place'].' '.$row['tosee']. '<br>';

Then you can use the $out variable anywhere else... Even for these...

$destination .= $row["destination"]; 
$place .= $row["place"]; 

Since well....are in the while loop

Kevin Gales
  • 532
  • 4
  • 9
-2

I found a few small errors, you had a ',' instead of a '.' to concatenate a variable.

if($count = $result->num_rows) {
    echo '<p>' . $count . '</p><br><br>';
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        
        echo $row['destination'].' '.$row['place'].' '.$row['tosee']. '<br>';
         
    //$destination =$row["destination"]; 
    //$place =$row["place"]; 
    //$destination =$row["destination"]; 
    //$place =$row["place"]; 
    }
}

and for php comments it is: //

Jatniel
  • 1,967
  • 2
  • 19
  • 27
  • 3
    That's an `echo` statement.... Commas work just as well and arguably are faster – Steven Apr 10 '21 at 19:19
  • **ok, thanks @Steven for the info**, although I prefer the do. In this case your comment is interesting. I share another post that effectively discusses this [link](https://stackoverflow.com/questions/1466408/difference-between-period-and-comma-when-concatenating-with-echo-versus-return) – Jatniel Apr 10 '21 at 19:28
  • is it possible to use variables in while loop? or is it have to be with echo? – andrewpuni Apr 10 '21 at 19:32
  • you can use the vessel variable, e.g. $row[''] to give value to other variables, or save information in the database, for example. Do not just echo – Jatniel Apr 10 '21 at 19:35
  • can you give me example how can i do it?i mean,how can comment echo and use variable? as i ask in my question? – andrewpuni Apr 10 '21 at 19:47
  • you can save in the variable $destination `$destination =$row["destination"];` and you can `echo $destination` always within while. If you need to do something else, for example, you could store it in an array if necessary. – Jatniel Apr 10 '21 at 19:55