Jump to content

Turn sql values into variables.


wright67uk

Recommended Posts

Is there a way to add to this script so that 'name', 'phone' , 'email' and 'webaddress' are also variables.

Im looking to put the variables into the gmap.php link below.

<?php
$subtype = $_GET['subtype'];
$subtype = ucwords (strtolower($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

your loop after the query is unsetting any keys from the resulting array if there is no value for it. So not every field could have a value. but as you query is pulling all of the field you ask for already then you just need to call like this example:

<?php
if( $result = mysql_query($query) ) {
echo mysql_error();
while($row = mysql_fetch_assoc($result) ) {
                  $name = $row['name'];
                  $phone = $row['phone'];
                  $email = $row['email'];
                  $webaddress= $row['WebAddress'];

	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

I think all you need is like this:??

$queryVars = array();
foreach ($row as $k => $v) {
    $queryVars[] = $k . '=' . urlencode($v);
}
$row['postcode_link'] = "<p class=link><a href='/gmap.php?" . implode('&', $queryVars) . "'>Map</a></p><br>";

 

you just replace this code:

if( !empty($row['postcode']) ) {
		$row['postcode_link'] = "<p class=link><a href='/gmap.php?postcode=" . urlencode($row['postcode']) . "'>Map</a></p><br>";
	}

 

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.