CyberShot Posted December 13, 2010 Share Posted December 13, 2010 I have a test database set up that I am working on. Right now, just some names like so firstName lastName companyName I have used phpmyadmin to insert names in the database. Right now just three names are inserted. One first and last name, one first name and one company name I am getting the results back in an associative array. How can I echo out the information with the break tag, or one field at a time? if I use a break tag, then the loop adds in extra blank lines when it gets to the first name and it's null. Can I test for null values and do it that way? I am a beginner so I have no idea what I am doing. $result = $mysql->query("select * from names") or die($mysql->error); if($result){ while($row = $result->fetch_assoc()){ $name = $row['firstName']; $lastName = $row['lastName']; $company = $row['companyName']; echo $name . " "; echo $lastName . " "; echo $company . " "; } } Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/ Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 a break statement is to get out of a loop early. what you want is EOL terminators and that depends on your OS/Browser echo "This will work in a browser <br />"; echo "This will work from cli \n"; // \n linux EOL, \r\n Windows EOL, \r Mac EOL echo "This will also work in cli". PHP_EOL; echo "This will work in browser and cli <br />". PHP_EOL; also note that php also has the function nl2br, which converts the EOL \n to a <br /> tag Echo nl2br("This String\nWill be split\nInto 3 lines"); Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146525 Share on other sites More sharing options...
CyberShot Posted December 13, 2010 Author Share Posted December 13, 2010 that kind of thing isn't working for me. I end up with this joe black john doe sears you see, there is a big gap between sears and the first last name. The reason is because of the <br> tag that I throw into the code to put each result row onto a new line in my code. I am trying to figure out how to get the code to ignore the nulls and put everything on the next line instead of several lines down Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146528 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 yer code has no <br /> or newlines. so how are we supposed to help, if your code snippet has nothing to do with the actual problem? as is your code will display all results on one line Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146537 Share on other sites More sharing options...
CyberShot Posted December 13, 2010 Author Share Posted December 13, 2010 I have been continuing to work on this issue. I took the <br> tags out trying to fix it. You are correct that my code doesn't not have any <br> tags in it. That is why I told you what I get when I use the br tags. yes, right now it spits the results out on one line. I am trying to figure out how to put each result on a seperate line without the code adding in extra blank lines between results as I showed you happens in the post above when I use br tags in this fashion echo $firstName . " " . $lastName . "<br />"; echo $companyName; If I do the above, I get this john doe Chrissy sears it gives me that because I left the last name blank on the second entry or whatever combination I use. It puts the company name further down the list with blank lines inbetween Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146538 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 $line= trim(implode(' ',array($firstName,$lastName))); if(!empty($line) $line.='<br />"; $companyName=trim($companyName); if(!empty($companyName) $line.=$companyName .'<br />"; echo $line; Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146540 Share on other sites More sharing options...
CyberShot Posted December 13, 2010 Author Share Posted December 13, 2010 that makes sense. thank you. I will try that. Quote Link to comment https://forums.phpfreaks.com/topic/221482-organizing-my-query-from-the-database/#findComment-1146542 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.