Jump to content

PHP MySQL Column Display Issue


phpinfo()

Recommended Posts

I have a database with one row and about 50 columns, where the inserted data is being displayed on a page. But a lot of the time not all columns are being used. So say only 25 columns have data, there will be 25 blanks spaces with an • symbol.

 

Is there a way to ignore to columns that have no data in them? Such as SELECT * from notices WHERE [columns have data]

 

 

 

<?php
$html="<table>";
$q="select * from notices";
$r=mysql_query($q);
while($o=mysql_fetch_object($r)){
foreach($o as $key=>$val){
$html.="<tr><td colspan='2' bgcolor='#896B45'><div style='padding:10px;'>&#8226; {$val}<br></div></td></tr>";
}}
$html.="</table>";
echo $html;
?>



CREATE TABLE `notices` (
  `id` int(11) NOT NULL auto_increment,
  `notice1` text,
  `notice2` text,
  `notice3` text,
  `notice4` text,
  `notice5` text,
  `notice6` text,
  `notice7` text,
  `notice8` text,
  `notice9` text,
  `notice10` text,
  `notice11` text,
  `notice12` text,
  `notice13` text,
  `notice14` text,
  `notice15` text,
  `notice16` text,
  `notice17` text,
  `notice18` text,
  `notice19` text,
  `notice20` text,
  `notice21` text,
  `notice22` text,
  `notice23` text,
  `notice24` text,
  `notice25` text,
  `notice26` text,
  `notice27` text,
  `notice28` text,
  `notice29` text,
  `notice30` text,
  `notice31` text,
  `notice32` text,
  `notice33` text,
  `notice34` text,
  `notice35` text,
  `notice36` text,
  `notice37` text,
  `notice38` text,
  `notice39` text,
  `notice40` text,
  `notice41` text,
  `notice42` text,
  `notice43` text,
  `notice44` text,
  `notice45` text,
  `notice46` text,
  `notice47` text,
  `notice48` text,
  `notice49` text,
  `notice50` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=40 DEFAULT CHARSET=utf8

 

 

I have an admin page for the posting of the date to the database:

<?php 
include_once($_SERVER["DOCUMENT_ROOT"] . "/main.inc.php");
$page_content = db_select("select * from notices");
$pc = $page_content[0];
$ff=$_POST;
if ($ff['submit']) {
$insc="UPDATE notices SET notice1='".$ff['notice1']."' , notice2='".$ff['notice2']."' , notice3='".$ff['notice3']."' , notice4='".$ff['notice4']."' , notice5='".$ff['notice5']."' , notice6='".$ff['notice6']."' , notice7='".$ff['notice7']."' , notice8='".$ff['notice8']."' , notice9='".$ff['notice9']."' , notice10='".$ff['notice10']."' , notice11='".$ff['notice11']."' , notice12='".$ff['notice12']."' , notice13='".$ff['notice13']."' , notice14='".$ff['notice14']."' , notice15='".$ff['notice15']."' , notice16='".$ff['notice16']."' , notice17='".$ff['notice17']."' , notice18='".$ff['notice18']."' , notice19='".$ff['notice19']."' , notice20='".$ff['notice20']."' , notice21='".$ff['notice21']."' , notice22='".$ff['notice22']."' , notice23='".$ff['notice23']."' , notice24='".$ff['notice24']."' , notice25='".$ff['notice25']."' , notice26='".$ff['notice26']."' , notice27='".$ff['notice27']."' , notice28='".$ff['notice28']."' , notice29='".$ff['notice29']."' , notice30='".$ff['notice30']."' , notice31='".$ff['notice31']."' , notice32='".$ff['notice32']."' , notice33='".$ff['notice33']."' , notice34='".$ff['notice34']."' , notice35='".$ff['notice35']."' , notice36='".$ff['notice36']."' , notice37='".$ff['notice37']."' , notice38='".$ff['notice38']."' , notice39='".$ff['notice39']."' , notice40='".$ff['notice40']."' , notice41='".$ff['notice41']."' , notice42='".$ff['notice42']."' , notice43='".$ff['notice43']."' , notice44='".$ff['notice44']."' , notice45='".$ff['notice45']."' , notice46='".$ff['notice46']."' , notice47='".$ff['notice47']."' , notice48='".$ff['notice48']."' , notice49='".$ff['notice49']."' , notice50='".$ff['notice50']."' , id='1' WHERE id='1'";

mysql_query($insc);	

}
?>
<html>
<head>
</head>
<body>
<form name="noticesform" method="post" action="notices_admin.php" enctype="multipart/form-data">
                                    
                                      <textarea name='notice1' cols='110' rows='3' class='input'><?=$pc["notice1"]?>
</textarea>
                                      <textarea name='notice2' cols='110' rows='3' class='input'><?=$pc["notice2"]?>
</textarea>
                                      <textarea name='notice3' cols='110' rows='3' class='input'><?=$pc["notice3"]?>
</textarea>
                                      <textarea name='notice4' cols='110' rows='3' class='input'><?=$pc["notice4"]?>
</textarea>
                                      <textarea name='notice5' cols='110' rows='3' class='input'><?=$pc["notice5"]?>
</textarea>
                                      <textarea name='notice6' cols='110' rows='3' class='input'><?=$pc["notice6"]?>
</textarea>
                                      <textarea name='notice7' cols='110' rows='3' class='input'><?=$pc["notice7"]?>
</textarea>
                                      <textarea name='notice8' cols='110' rows='3' class='input'><?=$pc["notice8"]?>
</textarea>
                                      <textarea name='notice9' cols='110' rows='3' class='input'><?=$pc["notice9"]?>
</textarea>
                                      <textarea name='notice10' cols='110' rows='3' class='input'><?=$pc["notice10"]?>
</textarea>
                                      <textarea name='notice11' cols='110' rows='3' class='input'><?=$pc["notice11"]?>
</textarea>
                                      <textarea name='notice12' cols='110' rows='3' class='input'><?=$pc["notice12"]?>
</textarea>
                                      <textarea name='notice13' cols='110' rows='3' class='input'><?=$pc["notice13"]?>
</textarea>
                                      <textarea name='notice14' cols='110' rows='3' class='input'><?=$pc["notice14"]?>
</textarea>
                                      <textarea name='notice15' cols='110' rows='3' class='input'><?=$pc["notice15"]?>
</textarea>
                                      <textarea name='notice16' cols='110' rows='3' class='input'><?=$pc["notice16"]?>
</textarea>
                                      <textarea name='notice17' cols='110' rows='3' class='input'><?=$pc["notice17"]?>
</textarea>
                                      <textarea name='notice18' cols='110' rows='3' class='input'><?=$pc["notice18"]?>
</textarea>
                                      <textarea name='notice19' cols='110' rows='3' class='input'><?=$pc["notice19"]?>
</textarea>
                                      <textarea name='notice20' cols='110' rows='3' class='input'><?=$pc["notice20"]?>
</textarea>
                                      <textarea name='notice21' cols='110' rows='3' class='input'><?=$pc["notice21"]?>
</textarea>
                                      <textarea name='notice22' cols='110' rows='3' class='input'><?=$pc["notice22"]?>
</textarea>
                                      <textarea name='notice23' cols='110' rows='3' class='input'><?=$pc["notice23"]?>
</textarea>
                                      <textarea name='notice24' cols='110' rows='3' class='input'><?=$pc["notice24"]?>
</textarea>
                                      <textarea name='notice25' cols='110' rows='3' class='input'><?=$pc["notice25"]?>
</textarea>
                                      <textarea name='notice26' cols='110' rows='3' class='input'><?=$pc["notice26"]?>
</textarea>
                                      <textarea name='notice27' cols='110' rows='3' class='input'><?=$pc["notice27"]?>
</textarea>
                                      <textarea name='notice28' cols='110' rows='3' class='input'><?=$pc["notice28"]?>
</textarea>
                                      <textarea name='notice29' cols='110' rows='3' class='input'><?=$pc["notice29"]?>
</textarea>
                                      <textarea name='notice30' cols='110' rows='3' class='input'><?=$pc["notice30"]?>
</textarea>
                                      <textarea name='notice31' cols='110' rows='3' class='input'><?=$pc["notice31"]?>
</textarea>
                                      <textarea name='notice32' cols='110' rows='3' class='input'><?=$pc["notice32"]?>
</textarea>
                                      <textarea name='notice33' cols='110' rows='3' class='input'><?=$pc["notice33"]?>
</textarea>
                                      <textarea name='notice34' cols='110' rows='3' class='input'><?=$pc["notice34"]?>
</textarea>
                                      <textarea name='notice35' cols='110' rows='3' class='input'><?=$pc["notice35"]?>
</textarea>
                                      <textarea name='notice36' cols='110' rows='3' class='input'><?=$pc["notice36"]?>
</textarea>
                                      <textarea name='notice37' cols='110' rows='3' class='input'><?=$pc["notice37"]?>
</textarea>
                                      <textarea name='notice38' cols='110' rows='3' class='input'><?=$pc["notice38"]?>
</textarea>
                                      <textarea name='notice39' cols='110' rows='3' class='input'><?=$pc["notice39"]?>
</textarea>
                                      <textarea name='notice40' cols='110' rows='3' class='input'><?=$pc["notice40"]?>
</textarea>
                                      <textarea name='notice41' cols='110' rows='3' class='input'><?=$pc["notice41"]?>
</textarea>
                                      <textarea name='notice42' cols='110' rows='3' class='input'><?=$pc["notice42"]?>
</textarea>
                                      <textarea name='notice43' cols='110' rows='3' class='input'><?=$pc["notice43"]?>
</textarea>
                                      <textarea name='notice44' cols='110' rows='3' class='input'><?=$pc["notice44"]?>
</textarea>
                                      <textarea name='notice45' cols='110' rows='3' class='input'><?=$pc["notice45"]?>
</textarea>
                                      <textarea name='notice46' cols='110' rows='3' class='input'><?=$pc["notice46"]?>
</textarea>
                                      <textarea name='notice47' cols='110' rows='3' class='input'><?=$pc["notice47"]?>
</textarea>
                                      <textarea name='notice48' cols='110' rows='3' class='input'><?=$pc["notice48"]?>
</textarea>
                                      <textarea name='notice49' cols='110' rows='3' class='input'><?=$pc["notice49"]?>
</textarea>
                                      <textarea name='notice50' cols='110' rows='3' class='input'><?=$pc["notice50"]?>
</textarea>
                                      
                                      <input type="submit" name="submit" value="Click here to update" class="input">
                                    </form>

</body>
</html>

Link to comment
Share on other sites

Had you set up (or choose to alter) your table so that it makes better use of the database engine, you can do what you are asking easily in the query and you could have saved typing all the repeated information for each column.

 

The following alternate data method is tested -

 

The table:

CREATE TABLE `notices` (
  `id` int(11) NOT NULL auto_increment,
  `notice` text,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

 

display code:

<?php
$html="<table>";
$q="select * from notices WHERE notice != '' order by id";
$r=mysql_query($q);
while($o=mysql_fetch_object($r)){
$html.="<tr><td colspan='2' bgcolor='#896B45'><div style='padding:10px;'>&#38;#8226; {$o->notice}<br></div></td></tr>";
}
$html.="</table>";

 

notices_admin.php code:

<?php 
$max_fields = 50; // number of form fields to show
$ff=$_POST;
// multi value REPLACE query - REPLACE INTO notices (id,notice) VALUES (x,'notice'),(y,'notice'),(z,'notice'),...
if (isset($ff['submit'])) {
//post data looks like: $ff['notice'][1..50] The keys (1-50) are the table id's.
$query = '';
foreach($ff['notice'] as $key => $value){
	$query .= "($key,'$value'),";
}
$query = "REPLACE INTO notices (id,notice) VALUES " . rtrim($query,',');
mysql_query($query);
}
?>
<html>
<head>
</head>
<body>
<form name="noticesform" method="post" action="notices_admin.php" enctype="multipart/form-data">
<?php
$page_content = db_select("select * from notices order by id"); // get existing data (after it has been updated(repalced) above
$i = 1; // index counter
$content = '';
foreach($page_content as $pc){
$content .= "Notice: $i <textarea name='notice[$i]' cols='110' rows='3' class='input'>{$pc["notice"]}</textarea><br />\n";
$i++;
}
// produce remainder of fields (when less than the max exist in the table)
for($j = $i; $j <= $max_fields; $j++){
	$content .= "Notice: $j <textarea name='notice[$j]' cols='110' rows='3' class='input'></textarea><br />\n";
}
echo $content;
?>
<input type="submit" name="submit" value="Click here to update" class="input">
</form>
</body>
</html>
echo $html;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.