MP145 Posted November 16, 2008 Share Posted November 16, 2008 Here is my code. <?php //Connects to Database mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database"); $db = mysql_select_db("$dbname") or die("Unable to select db"); //Retrieves data from MySQL $data = mysql_query("SELECT * FROM members ORDER BY id LIMIT 3") or die(mysql_error()); $num=mysql_numrows($data); if ($num==0) { echo "No Data"; } else { //Puts it into an array while($info = mysql_fetch_array( $data )) { echo ''.$info['street1'].'<br>'.$info['street2'].'<br>'.$info['street3'].'<br>'.$info['pcode'].'<br>'.$info['city'].'<br>'.$info['state'].'<br>'; } } ?> I am trying to list down a person's address. It does the job but when there is no data for the field street2 or street3 it still prints the <br> and there will be a gap between street 1 and the pcode. how can i overcome this ? do i have to use the if else ? or is there any other way? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 yea i think you will need else if while($info = mysql_fetch_array($data)){ echo $info['street1']."<br>"; echo $info['street2'] == '' ? '' : $info['street2']."<br>"; echo $info['street3'] == '' ? '' : $info['street3']."<br>"; echo $info['pcode'].'<br>'.$info['city'].'<br>'.$info['state'].'<br>'; } Scott. Quote Link to comment Share on other sites More sharing options...
MP145 Posted November 16, 2008 Author Share Posted November 16, 2008 Thank you very much ratcateme for the reply and thanks a million for the code, works like a charm with short line, where else i went and did this. <?php include("ullemasuk.php"); //Connects to Database mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database"); $db = mysql_select_db("$dbname") or die("Unable to select db"); //Retrieves data from MySQL $data = mysql_query("SELECT * FROM members ORDER BY id LIMIT 3") or die(mysql_error()); $num=mysql_numrows($data); if ($num==0) { echo "No Data"; } else { //Puts it into an array while($info = mysql_fetch_array( $data )) { echo ''.$info['street1'].'<br>'; if (!empty($info['street2'])) { echo ''.$info['street2'].'<br>'; } if (!empty($info['street3'])) { echo ''.$info['street3'].'<br>'; } if (!empty($info['pcode'])) { echo ''.$info['pcode'].'<br>'; } if (!empty($info['city'])) { echo ''.$info['city'].'<br>'; } if (!empty($info['state'])) { echo ''.$info['state'].'<br>'; } else { echo null; } echo '<br>'; } } ?> Works also but, i'll definitely use your code . Thanks again. Quote Link to comment 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.