wright67uk Posted June 17, 2011 Share Posted June 17, 2011 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239615-turn-sql-values-into-variables/ Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239615-turn-sql-values-into-variables/#findComment-1230926 Share on other sites More sharing options...
iblood Posted June 17, 2011 Share Posted June 17, 2011 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/239615-turn-sql-values-into-variables/#findComment-1231068 Share on other sites More sharing options...
wright67uk Posted June 17, 2011 Author Share Posted June 17, 2011 Thankyou for the suggestions. Is there a third person that would like to confirm which of these ways is using the best practice? Quote Link to comment https://forums.phpfreaks.com/topic/239615-turn-sql-values-into-variables/#findComment-1231255 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.