imarockstar Posted April 18, 2008 Share Posted April 18, 2008 I have a bunch of code I am displaying within 1 TABLE. Some rows are empty and some are not .. As you can see here : Ok ... I am displaying these 6 fields at the bottom of the output .. title1 html1 title2 html2 title3 html3 There will always be data in "title1 and html1" but not always in the other 2. How can I get it not to display the code if there is nothing in the field ?? <?php $db = mysql_connect("localhost", "***", "**") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("***",$db)) die("No database selected."); //set the number of columns $columns = 3; //select the table if($_GET['id']){$query="select * from html where id='$_GET[id]'";} else{$query="SELECT * FROM html";} $result = mysql_query($query); //we add this line because we need to know the number of rows $num_rows = mysql_num_rows($result); echo "<TABLE class='glitter'>\n"; //changed this to a for loop so we can use the number of rows for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } echo "<TD valign='top'> <div class='code_box'> <div class='code_header'><h4 class='code_header'>". $row ['menu_name'] ."</h4></div> <div class='code_desc'> ". $row ['content'] ." </div> <div class='code_title'>". $row ['title1'] ."</div> <textarea class='code_area'> ".$row['html1']." </textarea> <div class='code_title'>". $row ['title2'] ."</div> <textarea class='code_area'> ".$row['html2']." </textarea> <div class='code_title'>". $row ['title3'] ."</div> <textarea class='code_area'> ".$row['html3']." </textarea> </div> </TD><td width='5'></td>\n"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "</TR><tr><td height='25'></td></tr>\n"; } } echo "</TABLE>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/ Share on other sites More sharing options...
Zhadus Posted April 18, 2008 Share Posted April 18, 2008 Replace <?php echo "<TD valign='top'> <div class='code_box'> <div class='code_header'><h4 class='code_header'>". $row ['menu_name'] ."</h4></div> <div class='code_desc'> ". $row ['content'] ." </div> <div class='code_title'>". $row ['title1'] ."</div> <textarea class='code_area'> ".$row['html1']." </textarea> <div class='code_title'>". $row ['title2'] ."</div> <textarea class='code_area'> ".$row['html2']." </textarea> <div class='code_title'>". $row ['title3'] ."</div> <textarea class='code_area'> ".$row['html3']." </textarea> </div> </TD><td width='5'></td>\n"; ?> With <?php echo "<TD valign='top'> <div class='code_box'> <div class='code_header'><h4 class='code_header'>". $row ['menu_name'] ."</h4></div> <div class='code_desc'> ". $row ['content'] ." </div>"; for ($x = 1; $x < 4; $x++) { $title = 'title' . $x; $html = 'html' . $x; if (isset($row[$title])) { echo '<div class=\"code_title\">'. $row[$title] .'</div> <textarea class=\"code_area\"> '.$row[$html].' </textarea>'; } } echo "</div> </TD><td width='5'></td>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/#findComment-520606 Share on other sites More sharing options...
imarockstar Posted April 18, 2008 Author Share Posted April 18, 2008 that did not seam to work .. this is what it gave me : http://themyspacesource.com/html_code_view.php?id=20 b Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/#findComment-520618 Share on other sites More sharing options...
Zhadus Posted April 18, 2008 Share Posted April 18, 2008 Ok, on my code remove the if isset() line and replace with: $tval = trim($row[$title]); if (!empty($tval)) { That will trim whitespace from the title so if you have a string of " " it will come up empty and then checks to make sure the string is NOT empty. Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/#findComment-520626 Share on other sites More sharing options...
craygo Posted April 18, 2008 Share Posted April 18, 2008 try this <?php echo "<TD valign='top'>\n"; $html = " <div class='code_box'> <div class='code_header'><h4 class='code_header'>". $row ['menu_name'] ."</h4></div> <div class='code_desc'> ". $row ['content'] ." </div>\n"; $html .= " <div class='code_title'>". $row ['title1'] ."</div> <textarea class='code_area'> ".$row['html1']." </textarea>\n"; $html .= !empty($row['title2']) ? " <div class='code_title'>". $row ['title2'] ."</div> <textarea class='code_area'> ".$row['html2']." </textarea>" : ""; $html .= !empty($row['title3']) ? "<div class='code_title'>". $row ['title3'] ."</div> <textarea class='code_area'> ".$row['html3']." </textarea>" : ""; echo $html; echo " </div> </TD><td width='5'></td>\n"; ?> Ray Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/#findComment-520628 Share on other sites More sharing options...
imarockstar Posted April 18, 2008 Author Share Posted April 18, 2008 ok thanks man .. thats bad ass !!!! b Link to comment https://forums.phpfreaks.com/topic/101749-little-bit-of-mysqlphp-output-help-please/#findComment-520638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.