B5ae59b19cc356df333baffffb9f2c91

Hi, I am just scratching the surface with PHP. I am trying to write a conditional statement with PHP to generate a div block with different classes based on the page I am on.

I would like to display the block with class="slideshow" if the ID is 8 or the empty div block with class="header".

It's escaping the quotes that's getting me, I think. :-) Thanks in advance!

<?php if ( the_ID() == 8) {
echo "<div id='menu_show' class='slideshow'>
  <img src='http://www.someone.com/wp-content/themes/True%20Nature/img/rotate1.jpg' alt='A picture' width='748' height='245' />
</div>"
else {
echo "<div class='header'></div>"
}
?>

Refactorings

No refactoring yet !

441c4f02db55ef2cbe96027af7012e01

techietim

December 7, 2007, December 07, 2007 19:31, permalink

2 ratings. Login to rate!

This is one way to shorten it

<?php 
echo (the_ID() == 8) ? '<div id="menu_show" class="slideshow"><img src="http://example.org/image.gif" alt="A picture" width="748" height="245" /></div>':'<div class="header"></div>';
?>
B849433e0e1a3f4ca0cf7cc55b8acd53

Casper

December 7, 2007, December 07, 2007 19:35, permalink

2 ratings. Login to rate!

You have a missing close brace "}" before the else statement.

Also when you have a lot of HTML code it's easier not to "echo" it, just type it out outside the PHP block instead, and keep the PHP code inside the <? ?> blocks. See below alternative 1.

Or..if you have a big block of text you can use what's called the heredoc operator. This will let you type in quotes and any text until the end of the heredoc. See alternative 2. Not as "clean" as alternative 1 perhaps, but demonstrates the heredoc operator.

<?php if (the_ID() == 8) { ?>
  <div id="menu_show" class="slideshow">
    <img src="..." />
  </div>
<?php } else { ?>
  <div class="header"></div>
<?php } ?>
<?php if (the_ID() == 8) {
  echo <<<EOL  // <- We use the heredoc operator. Notice 3 times '<'
    <div id="menu_show" class="...">
      ...
    </div>
EOL;  // End the heredoc block. Has to be the first characters on the line,
      // so don't indent it.
} else {
  echo "<div class=\"header\"></div>";
}
?>
B5ae59b19cc356df333baffffb9f2c91

K

December 7, 2007, December 07, 2007 19:43, permalink

No rating. Login to rate!

Tim, thanks a lot! This is very helpful.

Casper, I like your first alternative. That's gonna help me as I am going to check for multiple conditions with elseif and extend the blocks.

The second alternative might actually come in handy should I use CASE statement for the same.

I had blogged about RefactorMyCode before at Shankrila.com but this is the first time I have posted on here. I am going to literally live here. You guys rock! :-)

B5ae59b19cc356df333baffffb9f2c91

K

December 7, 2007, December 07, 2007 19:44, permalink

No rating. Login to rate!

Can I ask you guys a favor and ask you to edit the url in the image to someone.com. That's really stupid of me to post the domain name!

0481b9ea2b2aad6844b33dbca2d7fe0e

Paul Kemper

December 8, 2007, December 08, 2007 08:57, permalink

3 ratings. Login to rate!

I suggest that you adhere to the following rules concerning the use of ' and "

1. Never put variables inside quotes (which you adhere to)
2. Never use " to enclose you strings, except in these two cases:
a. Your string has control characters like \n and \t
b. Your string is a SQL statement

That way, you can use the " to enclose attribute values in your HTML/XML strings, without the headaches

Also note that HEREDOC processing in PHP is quite time consuming, so try to avoid it.

Furthermore, a construct like if () ... else ... is clearer when reviewing your code than the ..?..:.. construct. If you have big pieces of code or strings to process, best use the if..else... construct. Consider using ..?..:.. only if there are two clear, small options (e.g. $name = ($_POST['name']==='')?'default':$_POST['name']; ). When the code gets executed, it is not slower or faster than ..?..:..

9bb39a91d11502e243caf099e79683f1

jenicun

April 10, 2008, April 10, 2008 02:42, permalink

No rating. Login to rate!

Sorry for putting it in the wrong topics,and of course,my bad english lol.can someone help me too ? can div statement be put we use php function ( we usually use javascript function ), but for somereason, i must combine the div tags with php function
thanks....

944c3fe0a9318990b75a3fb62d126e42

Habibur Rahman

December 28, 2008, December 28, 2008 14:35, permalink

No rating. Login to rate!

I’m novice programmer. In future I try to develop my programming skill on PHP but the following code not work (heredoc operator).

<?php   
  $link=mysql_connect("localhost","root","12345678") or die(mysql_error());
  
  mysql_select_db("moviesite") or die(mysql_error());
  
  $query="select movie_name,movie_director,movie_leadactor from movie";
  
  $result=mysql_query($query,$link) or die(mysql_error());
    
  $num_movie=mysql_num_rows($result); 
  
  $movie_header=<<<EOD
  <h2><center> Movie Review Database </center></h2>
  <table width="70%" border="1" cellpadding="2" cellspacing="2" align="center">
   <tr>
        <th>Movie Title</th>
	<th>Movie Director</th>
	<th>Movie Lead Actor</th>
   </tr>
  EOD;

  $movie_details='';
  while($row=mysql_fetch_array($result))
  {
    $movie_name=$row['movie_name'];
    $movie_director=$row['movie_director'];
    $movie_leadactor=$row['movie_leadactor'];

    $movie_details.=<<<EOD
	<tr>
	   <td>$movie_name</td>
	   <td>$movie_director</td>
	   <td>$movie_leadactor</td>
	</tr>
	EOD;
  }
  
  $movie_details.=<<<EOD 
  <tr>
      <td>&nbsp;</td>
  </tr>
  <tr>
      <td>Total : $num_movies movies</td>
  </tr>
  EOD; 
  
  $movie_footer="</table>";
  
  $movie=<<<MOVIE
            $movie_header
	    $movie_details
	    $movie_footer
  MOVIE; 

 echo "There are $num_movie movies in our database";
 echo $movie;   
 </table>			 
?>
B38fa6b064415a27205f19e78ebcb328

topoulp

April 19, 2009, April 19, 2009 06:31, permalink

No rating. Login to rate!

<a href=http://wiki.datasyncsuite.com/ReleaseNotes/GogexMogejab?action=AttachFile&do=get&target=ticket-to-sydney.txt>southwest airlines discount code</a> flights to bmi inside .is required for should Before las vegas airline tickets .tickets to egypt or someone <a href=http://www.astro.uni-bonn.de/boawiki/watchepay.com/XajuseaWoayv?action=AttachFile&do=get&target=tickets-to-europe.txt>tickets to europe</a> <a href=http://packagekit.freedesktop.org/wiki/TitleIndex/MekeguNusar?action=AttachFile&do=get&target=tickets-to-ghana.txt>cheap ticket from dallas</a> .Why ought to lowest airfare to las vegas take us .which delta airlines schedules <a href=http://linusera.org/wiki/EisihuxKotabev?action=AttachFile&do=get&target=tickets-from-guatemala.txt>austrian airlines hotel rules</a> tickets to dallas i'm with malaysia airline <a href=http://pdsl.cenditel.gob.ve/wiki/FrontPage/SavifoxKinezej?action=AttachFile&do=get&target=tickets-to-cairo.txt>frontier airlines news</a> malaysia airlines wallpapers could airline tickets deals a search <a href=http://nycpython.org/MeetingOct2008/CiauboQizyqel?action=AttachFile&do=get&target=tickets-to-nicaragua.txt>low airline fares</a> <a href=http://2007sdsu.on-wiki.net/Quotes/VeteseFocop?action=AttachFile&do=get&target=tickets-from-lima.txt>tickets from lima</a> .So may be last minute cheap flights but las vegas vacation packages .or airfares cheap discounts .<a href=http://pdsl.cenditel.gob.ve/wiki/FrontPage/SavifoxKinezej?action=AttachFile&do=get&target=tickets-to-cairo.txt>super cheap airline tickets</a> In what are low cost airlines and this is the best resource on cheap air line flights <a href=http://estilingue.sarava.org/moin/Yscambau/KyfurFomolag?action=AttachFile&do=get&target=tickets-to-bangkok.txt>hawaii vacations</a> indian airlines fares about .Come to Ok, here which air force 1 .cheap flights from athens After <a href=http://fs.hansung.ac.kr/embeddedwiki/HelpContents/HevuzumWyvywef?action=AttachFile&do=get&target=tickets-to-england.txt>tickets to england</a> <a href=http://severncarpool.com/BobGraw/NukubPeqos?action=AttachFile&do=get&target=tickets-to-catalina.txt>cheap virgin flights from usa to europe</a> .Such in the attached bmmi airlines to chicago Usualy .Well when should you buy international flight tickets <a href=http://estilingue.sarava.org/moin/Yscambau/KyfurFomolag?action=AttachFile&do=get&target=tickets-to-bangkok.txt>korean airlines to fly to palau</a> student airline travel discounts Heh, frontier airlines commercial Realy nice <a href=https://storm.canonical.com/MoinMoin/AuwagyDykaho?action=AttachFile&do=get&target=tickets-from-romania.txt>tickets from romania</a> <a href=http://wiki.saymoo.org/EvdlGems/MiscParts/WywybojAofahob?action=AttachFile&do=get&target=tickets-to-events.txt>lufthansa buying austrian airlines</a> .Information on And klm dutch airlines so around the world air fare .without cheap flights to london .<a href=https://gaia.lbl.gov/bir/MoinMoin/WefoxyNybuvew?action=AttachFile&do=get&target=tickets-from-frankfurt.txt>international airfares</a> Cool stuff - under cheapest flights from philadelphia to atlanta or recreational <a href=http://supfam.mrc-lmb.cam.ac.uk/MRCCompBio/GroupMeetings/JygasIydap?action=AttachFile&do=get&target=ticket-to-tokyo.txt>online travel agencies</a> <a href=http://wiki.saymoo.org/EvdlGems/MiscParts/WywybojAofahob?action=AttachFile&do=get&target=tickets-to-events.txt>lufthansa buying austrian airlines</a> .What is that was cheap flights singapore miami denver detroit singapore is focused on cheap flight tickets to amsterdam .should tickets from zimbabwe .<a href=http://python-matrix.on-wiki.net/QiroqCynywu?action=AttachFile&do=get&target=tickets-from-brazil.txt>air travel affiliates</a> so cheryl hussey frontier airlines now alaska airline <a href=http://aowiki.uab.es/wikitc/IGE.com/SiquqYijiv?action=AttachFile&do=get&target=tickets-to-kauai.txt>sas airlines</a> airfare bargain rates so so .inside under is required for cheap plane tickets from chicago to cambodia .travel trailers Other <a href=http://www.minix3.ucsc.edu/wikis/minix3/AssemblyLanguage/ZohasIigecyc?action=AttachFile&do=get&target=tickets-from-toronto.txt>cheap tickets from toronto to</a> <a href=http://www.2007trinityreu.on-wiki.net/Shotgun_Rules/GilofysXukoaif?action=AttachFile&do=get&target=tickets-from-jamaica.txt>travel packages</a> .Why could airplane tickets ought to .follow last minute international airfare <a href=http://fs.hansung.ac.kr/embeddedwiki/HelpContents/HevuzumWyvywef?action=AttachFile&do=get&target=tickets-to-england.txt>plane tickets to england</a> ding fares on southwest airlines defined .

522748524ad010358705b6852b81be4c

ds

April 21, 2009, April 21, 2009 10:06, permalink

No rating. Login to rate!

ds

D41d8cd98f00b204e9800998ecf8427e

qaa

May 13, 2009, May 13, 2009 06:03, permalink

No rating. Login to rate!
<html>
7d8f6da25b35316cd84812048565b09b

Fayaz

September 23, 2009, September 23, 2009 09:58, permalink

No rating. Login to rate!

hi.. i m new to php.. this is my program.. i think this will help you to connect the database and to save the students information..

<?php
    $connection = mysql_connect("localhost","root","fayaz");
        if(!$connection) {
        die ("database connection failed: " . mysql_error());
        }
            
    $db_select = mysql_select_db("fak",$connection);
        if (!$db_select) {
        die (" database selection failed: " . mysql_error());
        }
            
    $result = mysql_query("select * from students",$connection);
        if(!$result) {
            die ("database query failed: " . mysql_error());
        }
        
    $num = mysql_num_fields($result);
    
    echo "<table border=1 width=100% cellpadding=10 cellspacing=5>";
    echo "<tr>
            <th>si_no</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Age</th>
            <th>Department</th>
            <th>Degree</th>
            <th>Batch</th>
            <th>Address</th>
            <th>email</th>
            <th>Contact_no</th>
        </tr>";
    while($i = mysql_fetch_array($result))
        {
        echo "<tr>";
        for ($n=0; $n<$num; $n++) {
        echo "<td>".$i[$n]."</td>";
        }
        echo "</tr>";
        }
        echo "</table>";
        mysql_close($connection);
?>
F65b911435e7120931df28468488adab

krishna

February 26, 2010, February 26, 2010 04:31, permalink

No rating. Login to rate!

how to create div in php

Your refactoring





Format Copy from initial code

or Cancel