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
https://forums.phpfreaks.com/topic/239615-turn-sql-values-into-variables/
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);
}
}
?>

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>";
	}

 

Archived

This topic is now archived and is closed to further replies.

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