Jump to content

little bit of mysql/php output help please


imarockstar

Recommended Posts

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";



?>

 

 

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";
?>

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.