Echoing out results from second search correctly

#1

Hi Guys, well i have reached that point again where i am stuck .

My initial search does exactly what it says on the tin. the second search is supposed to search for the serial number in another table then display the latency in a row. I have tried a few different things to get this working but the closed was actually putting the second search in the output table. Any ideas greatful.

include './includes/db.php';
$initialsearch = mysqli_query($link, "SELECT * FROM assets limit 1");
while($row = mysqli_fetch_array($initialsearch)){
	$name = $row["name"];
	$model = $row["model"];
	$ip = $row["ip"];
	$serial = $row["serial"];

	$apsearch = mysqli_query($link, "SELECT latency FROM check_log where serial like '$serial' limit 5");
	while($row1 = mysqli_fetch_array($apsearch)){
		$latency = $row1["latency"];
	}

echo"
<tr>
<td><font color=$color size='6px'> $name</td>
<td><font color=$color size='6px'> $model</td>
<td><font color=$color size='6px'> $ip </td>
<td><font color=$color size='5px'> $latency ms</td>
</tr>
</body>
";  

My results from the above com out like this :

Name > Model > IP > Latency
Name > Model > IP > Latency
Name > Model > IP > Latency
Name > Model > IP > Latency
Name > Model > IP > Latency

what i want is :

Name > Model > IP > Latency > Latency > Latency > Latency > Latency

thanks all !

#2

In the second loop you override the $latency variable without showing the value.
My suggestion is to declare it as an empty string before the loop, then in the loop you append the latency to the variable.
Something like this:

	$latency = '';
	while($row1 = mysqli_fetch_array($apsearch)){
                if(!empty($latency)){
			$latency .= ' - ';
		}
		$latency .= $row1["latency"] . ' ms';
	}
#3

Thank you so much for the reply.

Really appreciated.

Paul

1 Like
#4

Sorry to ask again on the same topic but the results are coming out strange. The first line echos out the latency correctly, but the then second , third and so on are appending the results after each other. As i have never done this before i am stuck as to how to stop this.

<?php
include './includes/db.php';
$latency = '';
$initialsearch = mysqli_query($link, "SELECT * FROM assets");
while($row = mysqli_fetch_array($initialsearch)){
	$name = $row["name"];
	$model = $row["model"];
	$ip = $row["ip"];
	$serial = $row["serial"];

	$apsearch = mysqli_query($link, "SELECT latency FROM check_log where serial = '$serial' limit 5");	
	while($row1 = mysqli_fetch_array($apsearch)){
                if(!empty($latency)){
			$latency .= ' - ';
				}
		$latency .= $row1["latency"] . ' ms';
	}

echo"
<tr>
<td><font color=$color size='6px'> $name</td>
<td><font color=$color size='6px'> $model</td>
<td><font color=$color size='6px'> $ip </td>
<td><font color=$color size='5px'> $latency </td>
</tr>
</body>
";  
    }
|unit1|XR24|10.113.40.35|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms|
|---|---|---|---|
|unit2|XR24|10.113.40.37|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms - 0.812 ms - 0.72 ms - 0.764 ms - 0.702 ms - 0.774 ms|
|unit3|XR52|10.113.40.43|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms - 0.812 ms - 0.72 ms - 0.764 ms - 0.702 ms - 0.774 ms - 1.524 ms - 1.428 ms - 1.566 ms - 2.078 ms - 1.462 ms|
|unit4|XR24|10.113.40.46|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms - 0.812 ms - 0.72 ms - 0.764 ms - 0.702 ms - 0.774 ms - 1.524 ms - 1.428 ms - 1.566 ms - 2.078 ms - 1.462 ms - 1.004 ms - 1.057 ms - 0.892 ms - 0.874 ms - 0.893 ms|
|unit5|XR24|10.113.40.33|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms - 0.812 ms - 0.72 ms - 0.764 ms - 0.702 ms - 0.774 ms - 1.524 ms - 1.428 ms - 1.566 ms - 2.078 ms - 1.462 ms - 1.004 ms - 1.057 ms - 0.892 ms - 0.874 ms - 0.893 ms - 0.776 ms - 0.816 ms - 0.806 ms - 0.802 ms - 0.78 ms|
|unit6|XR62|10.113.40.62|0.953 ms - 0.655 ms - 0.681 ms - 0.728 ms - 0.704 ms - 0.812 ms - 0.72 ms - 0.764 ms - 0.702 ms - 0.774 ms - 1.524 ms - 1.428 ms - 1.566 ms - 2.078 ms - 1.462 ms - 1.004 ms - 1.057 ms - 0.892 ms - 0.874 ms - 0.893 ms - 0.776 ms - 0.816 ms - 0.806 ms - 0.802 ms - 0.78 ms - 1.075 ms - 1.256 ms - 1.173 ms - 0.977 ms - 1.139 ms|

Thanks in advance
Paul

#5

You putted $latency = ‘’; in the wrong location. Move it inside the first while.

#6

So, $latency’s scope exists outside of the loop. That means you are just going to keep adding on to it. You could either declare the variable within the loop that you need it in, or you need to ‘reset’ the value that it was previously storing.