Is there a better variable that I can use?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Is there a better variable that I can use?

Post by Lucnet »

I am very new with php and I am useing at the moment the $pagecontent.= in my code to get info to display. Is there a better way to do this.

You can see a page of my code below:

Code: Select all

<?
$status = (!empty($_GET['status']) ? $_GET['status'] : '%');
include "../inc/config.php";
$displaymessage =0;
switch($action) {
	case "approve":
	$displaymessage =1;
		db();
		mysql_query("UPDATE ls_testimonials SET approved='y' WHERE id=$id");
		db_close();
	break;
	case "hold":
	$displaymessage =2;
		db();
		mysql_query("UPDATE ls_testimonials SET approved='h' WHERE id=$id");
		db_close();
	break;
	case "delete":
	$displaymessage =3;
		db();
		mysql_query("DELETE FROM ls_testimonials WHERE id=$id");
		db_close();
	break;
	default:
	break;	
}
db();
$query = "SELECT * FROM ls_testimonials WHERE approved LIKE '$status'"; 
$formdata_res=mysql_query($query) or die("query failed: " . mysql_error()); 
$pagecontent.="<table width=760 border=0 cellspacing=0 cellpadding=0
  <tr>
  <td width=150></td>
	<td width=610>&nbsp;</td>
	</tr>
  <tr>
  <td valign=top><table width=150 border=1 cellpadding=2 cellspacing=2 bordercolor=#FFFFFF>
   <tr>
<td bordercolor=#000000 bgcolor=#CCCCCC><font size=2><b>Menu</b></font></td>
   </tr>
  <tr>
<td bordercolor=#000000><font size=2><p><a href=add.php>Add Testimonials</a><br>
<a href=manage.php?status=n>New Testimonials</a><br>
<a href=manage.php?status=h>On Hold Testimonials</a><br>
<a href=manage.php?status=y>Active Testimonials</a><br>
<a href=code.php>Generate Code</a></p></font></td>
   </tr>
</table></td>";
   $pagecontent.="<td valign=top>
     <table width=99% border=1 align=center cellpadding=0 cellspacing=1 bordercolor=#FFFFFF>";
while ($formdata=mysql_fetch_object($formdata_res)) {
	$client_id=htmlentities($formdata->client_id);
	$link_display=htmlentities($formdata->link_display);
	$email=htmlentities($formdata->email);
	$company=htmlentities($formdata->company);
	$testimonials=htmlentities($formdata->testimonials);
 $pagecontent.="<td bordercolor=#999999 bgcolor=#F1F1F1><font size=2><b>Client ID#:</b></font>{$client_id}</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Display:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Email:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Website:</b></font></td></tr>
  </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#E9EDF0><font size=2>{$link_display}</font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='mailto:{$email}'>{$email}</a></font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='{$company}' target=new>{$company}</a></font></td>
    </tr>
   <tr bordercolor=#666666>
<td colspan=2><font size=2><b>Testimonial:</b></font><textarea style=width=300; border: 1; rows=1>{$testimonials}</textarea></td>
<td><center><a href='?id={$formdata->id}&action=approve'><img src=../inc/skins/default/images/approve.gif width=17 height=17 border=0 ALT=Approve></a> 
<a href='?id={$formdata->id}&action=hold'><img src=../inc/skins/default/images/hold.gif width=17 height=17 border=0 ALT=Hold></a> 
<a href='?id={$formdata->id}&action=delete'><img src=../inc/skins/default/images/delete.gif width=17 height=17 border=0 ALT=Delete></a> </center></td>
  </tr>
  <tr> 
<td colspan=3>&nbsp;</td>
   </tr>";
}
if ($displaymessage == 0)
{ 
$pagecontent.="</table></td> 
                </tr> 
                <tr> 
                <td></td> 
                <td></td>"; 
} 
else if ($displaymessage ==1) 
{ 
$pagecontent.="</table></td> 
                 </tr> 
                 <tr> 
                   <td></td> 
                   <td>Item {$id} Approved</td>"; 
}
else if ($displaymessage ==2) 
{ 
$pagecontent.="</table></td> 
               </tr> 
               <tr> 
                <td></td> 
                <td>Item {$id} On Hold</td>";
}
else if ($displaymessage ==3) 
{ 
$pagecontent.="</table></td> 
                </tr> 
                <tr> 
                <td></td> 
                <td>Item {$id} Deleted</td>";
} 
//<!-- DO NOT REMOVE THIS CODE BELOW -->
$pagecontent.="</tr>
<tr>
    <td>&nbsp;</td>
    <td><div align=center>
        <p>&nbsp;</p>
        <p><font size=2>Powered by: <a href=http://www.lsscripts.com>LS Testimonials</a></font> 
          <br>
          <font size=2>Ver. 1.0</font> </p>
      </div></td>
  </tr>
</table>";
//<!-- COPYRIGHT CODES END -->
db_close();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Approval Page</title>
</head>
<body><?=$pagecontent;?></body>
</html>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

No better variable but as for the structure, there are a lot of different methods used. One mixes php and html all together but I find that really unreadable.

Personally I would build the main page in full at the end and in the front php section only build the bits which are variable. This separates the processing from the output which means you do not have to view processing code to work out the output and vice versa. This is turn makes your code more reusable. The page building is also useful when creating a site to build the basic framework. Then put in the processed information.

Where you have standard code for all your pages (copyright for instance) You may wish to put it in a separate file and readfile(filename) it. If the copyright changes you only have to change it in one place. Not all through your code.

Obviously all this is dependant on personal tastes and your requirements, I just find it useful.

Example:

Code: Select all

<?php
  $myhead='Bozo';
  $myvar='Hello';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Approval Page</title>
</head>
<body>
<table>
<tr>
  <td><?php =$myhead;?></td>
  <td><?php =$myvar;?></td>
</tr>
</table>
<?php readfile(copyright.html): ?>
</body>
</html>
You should also consider the use of style sheets rather than hardcoded format.
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

Thanks for the info. I plan on using CSS aventually. I am very new at php and want to make my script with the simplest possibly way. I aventually want to be able to use template also with this. But I'm just not that advanced yet.

Would it be better to use just all php?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I prefer the layout suggested for two reasons...

1) Readabilty
2) Ease of Modification.
3) Reusabilty

The simplest way is not always the simplest to maintain. You need good design to start with, the rest will follow.
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

Whats the difference between the
$myhead
$myvar
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

They are each separate variables used within the html code. If you needed to use one in two locations for instance you could.
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

When I added the $myvar I get an error. You can see the error here http://www.lsscripts.com/lst/admin/manage.php

Code: Select all

<?
$status = (!empty($_GET['status']) ? $_GET['status'] : '%');
include "../inc/config.php";
$displaymessage =0;
switch($action) {
	case "approve":
	$displaymessage =1;
		db();
		mysql_query("UPDATE ls_testimonials SET approved='y' WHERE id=$id");
		db_close();
	break;
	case "hold":
	$displaymessage =2;
		db();
		mysql_query("UPDATE ls_testimonials SET approved='h' WHERE id=$id");
		db_close();
	break;
	case "delete":
	$displaymessage =3;
		db();
		mysql_query("DELETE FROM ls_testimonials WHERE id=$id");
		db_close();
	break;
	default:
	break;	
}
db();
$query = "SELECT * FROM ls_testimonials WHERE approved LIKE '$status'"; 
$formdata_res=mysql_query($query) or die("query failed: " . mysql_error()); 
$myvar='<table width=760 border=0 cellspacing=0 cellpadding=0
  <tr>
  <td width=150></td>
	<td width=610>&nbsp;</td>
	</tr>
  <tr>
  <td valign=top><table width=150 border=1 cellpadding=2 cellspacing=2 bordercolor=#FFFFFF>
   <tr>
<td bordercolor=#000000 bgcolor=#CCCCCC><font size=2><b>Menu</b></font></td>
   </tr>
  <tr>
<td bordercolor=#000000><font size=2><p><a href=add.php>Add Testimonials</a><br>
<a href=manage.php?status=n>New Testimonials</a><br>
<a href=manage.php?status=h>On Hold Testimonials</a><br>
<a href=manage.php?status=y>Active Testimonials</a><br>
<a href=code.php>Generate Code</a></p></font></td>
   </tr>
</table></td>';
$myvar='<td valign=top>
     <table width=99% border=1 align=center cellpadding=0 cellspacing=1 bordercolor=#FFFFFF>';
while ($formdata=mysql_fetch_object($formdata_res)) {
	$client_id=htmlentities($formdata->client_id);
	$link_display=htmlentities($formdata->link_display);
	$email=htmlentities($formdata->email);
	$company=htmlentities($formdata->company);
	$testimonials=htmlentities($formdata->testimonials);
$myvar='<td bordercolor=#999999 bgcolor=#F1F1F1><font size=2><b>Client ID#:</b></font>{$client_id}</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Display:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Email:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Website:</b></font></td></tr>
  </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#E9EDF0><font size=2>{$link_display}</font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='mailto:{$email}'>{$email}</a></font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='{$company}' target=new>{$company}</a></font></td>
    </tr>
   <tr bordercolor=#666666>
<td colspan=2><font size=2><b>Testimonial:</b></font><textarea style=width=300; border: 1; rows=1>{$testimonials}</textarea></td>
<td><center><a href='?id={$formdata->id}&action=approve'><img src=../inc/skins/default/images/approve.gif width=17 height=17 border=0 ALT=Approve></a> 
<a href='?id={$formdata->id}&action=hold'><img src=../inc/skins/default/images/hold.gif width=17 height=17 border=0 ALT=Hold></a> 
<a href='?id={$formdata->id}&action=delete'><img src=../inc/skins/default/images/delete.gif width=17 height=17 border=0 ALT=Delete></a> </center></td>
  </tr>
  <tr> 
<td colspan=3>&nbsp;</td>
   </tr>';
}
if ($displaymessage == 0)
{ 
$myvar='</table></td> 
                </tr> 
                <tr> 
                <td></td> 
                <td></td>'; 
} 
else if ($displaymessage ==1) 
{ 
$myvar='</table></td> 
                 </tr> 
                 <tr> 
                   <td></td> 
                   <td>Item {$id} Approved</td>'; 
}
else if ($displaymessage ==2) 
{ 
$myvar='</table></td> 
               </tr> 
               <tr> 
                <td></td> 
                <td>Item {$id} On Hold</td>';
}
else if ($displaymessage ==3) 
{ 
$myvar='</table></td> 
                </tr> 
                <tr> 
                <td></td> 
                <td>Item {$id} Deleted</td>';
} 
//<!-- DO NOT REMOVE THIS CODE BELOW -->
$myvar='</tr>
<tr>
    <td>&nbsp;</td>
    <td><div align=center>
        <p>&nbsp;</p>
        <p><font size=2>Powered by: <a href=http://www.lsscripts.com>LS Testimonials</a></font> 
          <br>
          <font size=2>Ver. 1.0</font> </p>
      </div></td>
  </tr>
</table>';
//<!-- COPYRIGHT CODES END -->
db_close();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Approval Page</title>
</head>
<body><?php =$myvar;?></body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<?php =$myvar;?> isn't valid..

<?= $myvar; ?> or <?php echo $myvar; ?> generally are..
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

Well it isn't even making it to that $myvar. The error is on line 95. Where you see it say's " <a href='mailto:{$email}'>{$email}</a> ". Somethign about the quotation I think is messed up. Not sure what though.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Wow, this is a big string, but shouldn't this:

Code: Select all

<?php
$myvar='<td bordercolor=#999999 bgcolor=#F1F1F1><font size=2><b>Client ID#:</b></font>{$client_id}</td>
          <td> </td>
          <td> </td>
        </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Display:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Email:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Website:</b></font></td></tr>
  </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#E9EDF0><font size=2>{$link_display}</font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='mailto:{$email}'>{$email}</a></font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='{$company}' target=new>{$company}</a></font></td>
    </tr>
   <tr bordercolor=#666666>
<td colspan=2><font size=2><b>Testimonial:</b></font><textarea style=width=300; border: 1; rows=1>{$testimonials}</textarea></td>
<td><center><a href='?id={$formdata->id}&action=approve'><img src=../inc/skins/default/images/approve.gif width=17 height=17 border=0 ALT=Approve></a>
<a href='?id={$formdata->id}&action=hold'><img src=../inc/skins/default/images/hold.gif width=17 height=17 border=0 ALT=Hold></a>
<a href='?id={$formdata->id}&action=delete'><img src=../inc/skins/default/images/delete.gif width=17 height=17 border=0 ALT=Delete></a> </center></td>
  </tr>
  <tr>
<td colspan=3> </td>
   </tr>'; 
?>
be this?:

Code: Select all

<?php
$myvar='<td bordercolor=#999999 bgcolor=#F1F1F1><font size=2><b>Client ID#:</b></font>{$client_id}</td>
          <td> </td>
          <td> </td>
        </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Display:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Email:</b></font></td>
<td width=203 bgcolor=#F1F1F1><font size=2><b>Website:</b></font></td></tr>
  </tr>
  <tr bordercolor=#666666>
<td width=203 bgcolor=#E9EDF0><font size=2>{$link_display}</font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href=mailto:'.{$email}.'>{$email}</a></font></td>
<td width=203 bgcolor=#E9EDF0><font size=2><a href='{$company}' target=new>{$company}</a></font></td>
    </tr>
   <tr bordercolor=#666666>
<td colspan=2><font size=2><b>Testimonial:</b></font><textarea style=width=300; border: 1; rows=1>{$testimonials}</textarea></td>
<td><center><a href=?id='.{$formdata->id}.'&action=approve><img src=../inc/skins/default/images/approve.gif width=17 height=17 border=0 ALT=Approve></a>
<a href=?id='.{$formdata->id}.'&action=hold><img src=../inc/skins/default/images/hold.gif width=17 height=17 border=0 ALT=Hold></a>
<a href=?id='.{$formdata->id}.'&action=delete><img src=../inc/skins/default/images/delete.gif width=17 height=17 border=0 ALT=Delete></a> </center></td>
  </tr>
  <tr>
<td colspan=3> </td>
   </tr>'; 
?>
Notice:

Code: Select all

href='mailto:&#123;$email&#125;'
href=mailto:'&#123;$email&#125;'
href='?id=&#123;$formdata-&gt;id&#125;&amp;action=approve'
href=?id='.&#123;$formdata-&gt;id&#125;.'&amp;action=approve

href='?id='&#123;$formdata-&gt;id&#125;&amp;action=hold'
href=?id='.&#123;$formdata-&gt;id&#125;.'&amp;action=hold

href='?id=&#123;$formdata-&gt;id&#125;&amp;action=delete'
href=?id='.&#123;$formdata-&gt;id&#125;.'&amp;action=delete
Last edited by johnperkins21 on Thu Jul 15, 2004 1:44 pm, edited 2 times in total.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Well now that you are using the better way to not mix PHP and HTML you ran into the question of which "ticks" to use. If you are going to concatenate your output into a string and then echo it into a template or entier page... you must remember that HTML likes " " while PHP does not care, use ' '
Such that you can easily do the following:

Code: Select all

// Why ' ' are better with PHP and HTML
$cout .= '<a href="mailto:' . $email . '"></a>';
// This is the other ugly way to get the same result
$cout .= "<a href="mailto:" . $email . ""></a>";
// Yeah this second one i got confused myself!!!
Now...please keep PHP and HTML separate... it will help you integrate stuff without the need to use ob (output buffering)

Once I had to work with someone's code that had PHP and HTML mixed up I just had to take ob the whole thing and then use my template parser to inport the string... BIG PAIN and sloooooowwwww
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

I am still getting an error on line 95. You can see the error here http://www.lsscripts.com/lst/admin/manage.php The HTML is still there. When I take it out it doesn't work.

When I take this out:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>$URL_dir.</title>
</head>
<body><?=$myvar;?></body>
</html>

It doesn't work anymore.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is this in line 95?

Code: Select all

<a href='&#123;$company&#125;' target
User avatar
Lucnet
Forum Commoner
Posts: 61
Joined: Sat Jun 26, 2004 5:28 pm
Location: Alabama
Contact:

Post by Lucnet »

Thats line 96
Post Reply