I’m making a php page to draw information from a mysql database, and before I get stuck too headlong into it I thought I’d see if there is an easier way to do what I’m doing.
I have a MySql query that returns multiple rows, through which I can easily loop using a while loop and it will obviously loop through where there is relative “matching” data.
Each row that is pulled out can have between 1 and 16 columns of data that is different for each row, and the way I currently extracting valid information is hard coding “if” commands for each column so that only information that has been entered gets displayed, e.g:
if($column1 != ""){
$output .= "<div style='float: left;'>Column 1 Stuff</div>";
}
if($column2 != ""){
$output .= "<div style='float: left;'>Column 2 Stuff</div>";
}
This works fine, but it isn’t very dynamic and requires an update of the code if the Mysql Database Table expands, which it is. What I’d really like to be able to do it use a single bit of code, like a while loop, that performs the same procedure for every column that has data in it and ignores any blank cells. Is there a good way to achieve this efficiently that anyone knows of?
Also, just in case it’s a problem, my “div”s are all aligned left so that when they reach the end of their enclosing div they automatically wrap themselves to the next line neatly, so if there were 9 results there would be 3 rows of 3 “div” boxes. Is this a valid way of achieving this kind of result? I was looking into using a “break” command as with and answer in this post -> How to do a Horizontal looping in PHP, but I’ve had so many compatibility issues with pre IE9 tags that I am avoiding implementing any more where possible.
Thanks in advance your your help, if indeed you can!
Joe
Something like:
would handle any number of columns in the result set.