Jump to content

How to handle emtpy mysql query reults


wright67uk

Recommended Posts

Hello, Im sure this is an easy one, but I just cant get my head around this.

 

Where my results are displayed and there is a result without a webaddress or email address,

I dont want "br" to be output.

 

I pressume I need an If / else if statement for $row, but Im unsure of how I would incorporate this into my array?

and im also unsure of how to say IF $row == NULL, - dont display anything.

 

 

<?php 
$subtype = $_GET['subtype'];

echo "<h1>$subtype</h1>";
$result	 = mysql_query("SELECT name, phone, email, WebAddress, postcode FROM business WHERE subtype ='$subtype' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($row = mysql_fetch_array($result))
{
echo $row['name'] . "<br>";
echo $row ['phone'] . "<br>";
echo $row['email'] . "<br>";
echo $row['WebAddress'] . "<br>";
echo $row['postcode'] ;
$postcode = str_replace(' ','+',$row['postcode']); 
echo "<p class=link><a href='/gmap.php?postcode=$postcode'>Map</a></p><br>";
}
?>

Link to comment
Share on other sites

Since $row is an array, you can use foreach() to unset() any empties, then if postcode is not empty, format the link, add it to the $row array and implode it with <br> as the glue and echo it. For this to work properly, it was necessary to change mysql_fetch_array() to mysql_fetch_assoc(). This code isn't tested, but it may work as is. Give it a try and see what happens.

 

<?php
$subtype = $_GET['subtype'];

echo "<h1>$subtype</h1>";
$query = "SELECT name, phone, email, WebAddress, postcode FROM business WHERE subtype ='$subtype' AND confirmed ='Yes' ORDER BY name";
if( $result = mysql_query($query) ) {
echo mysql_error();
while($row = mysql_fetch_assoc($result) ) {
	foreach( $row as $k => $v ) {
		if( empty($v) ) {
			unset($row[$k]);
		}
	}
	if( !empty($row['postcode']) ) {
		$row['postcode_link'] = "<p class=link><a href='/gmap.php?postcode=" . urlencode($row['postcode']) . "'>Map</a></p><br>";
	}
	echo implode( '<br>', $row);
}
}

Link to comment
Share on other sites

Thankyou very much, that worked well first time.

 

Any chance you could talk through;

 

foreach( $row as $k => $v )

{if( empty($v) )

{unset($row[$k]);}

 

what is actually happening here.

I feel a bit cheeky using your code and not learning what its doing!

Link to comment
Share on other sites

When you foreach through the array, it assigns the value of the key to $k and the value of the actual element to $v. So for each element of the array, if $v is empty, it will unset the element completely. I.E. it isn't even there anymore.

 

And now that I think of it, there should also be a trim() there so a value that's nothing but whitespace won't be kept as a valid value. Add the line I commented in the code, and it should be good to go.

 

while($row = mysql_fetch_assoc($result) ) {
	$row = array_map('trim', $row); // <--- ADD THIS LINE 
	foreach( $row as $k => $v ) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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