Jump to content

echo table if field = x


Go to solution Solved by mac_gyver,

Recommended Posts

I am wanting to echo a table if a field = x and if not echo another table. Can this be done easily? I have tried but not having much luck. Any help would be appreciated by this "on the fly" learner.

 

Something like...   IF AdviserCode=X then

<table width="610" border="0" align="center" cellpadding="4" cellspacing="0"> 
      <tr class="text-sectionheading">
        <td width="100">Date </td>
        <td width="85">RCTI</td>
        <td width="85">Detail 1</td>
        <td width="85">Detail 2</td>
        <td width="85">Detail 3</td>
        <td width="85">Detail 4</td>
        <td width="85">Detail 5</td>
        </tr>
      
      <?php
			include("../edb.php");
			
				
			$result=mysql_query("SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode =".$_SESSION['uid']." order by DateUploaded DESC");

			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['id'];	
				echo"<tr>";
				echo"<td class='text-questions'>".$test['DateUploaded']."</td>";
				echo"<td><a class='".$test['RCTIcssclass']."'href =".$test['RCTIURL'].">".$test['RCTIImageType']."</a>";
				echo"<td><a class='".$test['Detail1cssclass']."'href =".$test['Detail1URL'].">".$test['Detail1ImageType']."</a>";
				echo"<td><a class='".$test['Detail2cssclass']."'href =".$test['Detail2URL'].">".$test['Detail2ImageType']."</a>";
				echo"<td><a class='".$test['Detail3cssclass']."'href =".$test['Detail3URL'].">".$test['Detail3ImageType']."</a>";
				echo"<td><a class='".$test['Detail4cssclass']."'href =".$test['Detail4URL'].">".$test['Detail4ImageType']."</a>";
				echo"<td><a class='".$test['Detail5cssclass']."'href =".$test['Detail5URL'].">".$test['Detail5ImageType']."</a>";
				echo "</tr>";
			}
			mysql_close($conn);
			?>

OR (same table as above minus a couple of colums)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/
Share on other sites

Unless I'm missing something, all you need to do is make an if/else block for those two conditions, put your existing code in the top half, then copy/paste it into the bottom half and make the couple changes you need. Literally

if($_SESSION['uid'] == 'X') {
	// your existing code
} else {
	// your existing code with a few changes
}

I thought that it could be that simple but the table with the column headings always seem to present problems.

 

I am obviously missing something really simple.

<?php
  if($_SESSION['uid'] == 'X') {
	     <table width="610" border="0" align="center" cellpadding="4" cellspacing="0"> 
      <tr class="text-sectionheading">
        <td width="100">Date </td>
        <td width="85">RCTI</td>
        <td width="85">Detail 1</td>
        <td width="85">Detail 2</td>
        <td width="85">Detail 3</td>
        <td width="85">Detail 4</td>
        <td width="85">Detail 5</td>
        </tr>
      
			include("../edb.php");
			
				
			$result=mysql_query("SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode =".$_SESSION['uid']." order by DateUploaded DESC");

			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['id'];	
				echo"<tr>";
				echo"<td class='text-questions'>".$test['DateUploaded']."</td>";
				echo"<td><a class='".$test['RCTIcssclass']."'href =".$test['RCTIURL'].">".$test['RCTIImageType']."</a>";
				echo"<td><a class='".$test['Detail1cssclass']."'href =".$test['Detail1URL'].">".$test['Detail1ImageType']."</a>";
				echo"<td><a class='".$test['Detail2cssclass']."'href =".$test['Detail2URL'].">".$test['Detail2ImageType']."</a>";
				echo"<td><a class='".$test['Detail3cssclass']."'href =".$test['Detail3URL'].">".$test['Detail3ImageType']."</a>";
				echo"<td><a class='".$test['Detail4cssclass']."'href =".$test['Detail4URL'].">".$test['Detail4ImageType']."</a>";
				echo"<td><a class='".$test['Detail5cssclass']."'href =".$test['Detail5URL'].">".$test['Detail5ImageType']."</a>";
				echo "</tr>";
			}
			mysql_close($conn);
  }else{
 <table width="610" border="0" align="center" cellpadding="4" cellspacing="0"> 
      <tr class="text-sectionheading">
        <td width="100">Date </td>
        <td width="85">RCTI</td>
        <td width="85">Detail 1</td>
        </tr>
      
			include("../edb.php");
			
				
			$result=mysql_query("SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode =".$_SESSION['uid']." order by DateUploaded DESC");

			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['id'];	
				echo"<tr>";
				echo"<td class='text-questions'>".$test['DateUploaded']."</td>";
				echo"<td><a class='".$test['RCTIcssclass']."'href =".$test['RCTIURL'].">".$test['RCTIImageType']."</a>";
				echo"<td><a class='".$test['Detail1cssclass']."'href =".$test['Detail1URL'].">".$test['Detail1ImageType']."</a>";
				echo "</tr>";}			mysql_close($conn);
			?>

You can't put HTML in the middle of PHP code. Use <?php to switch from HTML to PHP mode, then ?> to switch back.

<?php

if($_SESSION['uid'] == 'X') {

?>

html and some <?php code and ?> more html

<?php

} else {

?>

more html and <?php code and ?> stuff

<?php

}

?>
Edited by requinix

I am still not getting this to work.  The more I play with it the more confused I am and assuming the code is too. Please help this dunce.

<body>
<table width="680" border="0" align="center" cellpadding="4" cellspacing="0">
  <tr> <td> </td>
  </tr>
  <tr><td class="text-heading"><p>Commission Statements</p></td>
  </tr><tr><td>
	<?php
  if($_SESSION['uid'] == '1') {?>
  <table width="610" border="0" align="center" cellpadding="4" cellspacing="0"> 
      <tr class="text-sectionheading">
        <td width="100">Date </td>
        <td width="85">RCTI</td>
        <td width="85">Detail 1</td>
        <td width="85">Detail 2</td>
        <td width="85">Detail 3</td>
        <td width="85">Detail 4</td>
        <td width="85">Detail 5</td>
        </tr>
   <?   
			include("../edb.php");
			
				
			$result=mysql_query("SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode =".$_SESSION['uid']." order by DateUploaded DESC");

			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['id'];	
				echo"<tr>";
				echo"<td class='text-questions'>".$test['DateUploaded']."</td>";
				echo"<td><a class='".$test['RCTIcssclass']."'href =".$test['RCTIURL'].">".$test['RCTIImageType']."</a>";
				echo"<td><a class='".$test['Detail1cssclass']."'href =".$test['Detail1URL'].">".$test['Detail1ImageType']."</a>";
				echo"<td><a class='".$test['Detail2cssclass']."'href =".$test['Detail2URL'].">".$test['Detail2ImageType']."</a>";
				echo"<td><a class='".$test['Detail3cssclass']."'href =".$test['Detail3URL'].">".$test['Detail3ImageType']."</a>";
				echo"<td><a class='".$test['Detail4cssclass']."'href =".$test['Detail4URL'].">".$test['Detail4ImageType']."</a>";
				echo"<td><a class='".$test['Detail5cssclass']."'href =".$test['Detail5URL'].">".$test['Detail5ImageType']."</a>";
				echo "</tr>";
			}
			mysql_close($conn);
  ?></table> <?php }else{ ?>
<table width="270" border="0" align="center" cellpadding="4" cellspacing="0"> 
     <tr class="text-sectionheading">
       <td width="100">Date </td>
        <td width="85">RCTI</td>
        <td width="85">Detail</td>
  </tr>
    <?  
			include("../edb.php");
			
				
			$result=mysql_query("SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode =".$_SESSION['uid']." order by DateUploaded DESC");

			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['id'];	
				echo"<tr>";
				echo"<td class='text-questions'>".$test['DateUploaded']."</td>";
				echo"<td><a class='".$test['RCTIcssclass']."'href =".$test['RCTIURL'].">".$test['RCTIImageType']."</a>";
				echo"<td><a class='".$test['Detail1cssclass']."'href =".$test['Detail1URL'].">".$test['Detail1ImageType']."</a>";
				echo "</tr>";}			mysql_close($conn);
  }?>
</table>
</table>
</body>

you need to code for the differences, not the common things. you also need to put the 'business logic' up front, then put the 'presentation logic' at the end to eliminate duplication.

 

is the session uid being a one the only occurrence where you output the 2-5 details?

  • Solution

i'm guessing you are missing a sesison_start() statement that would allow any session variable to exist. do you have php's error_reporting/display_errors turned full on so that php will help you?

 

using the organization mentioned in your last thread and in this one, some untested logic that should work -

<?php
session_start();
include "../edb.php";
$query = "SELECT * FROM `afddocs_adviser_comm` WHERE AdviserCode ={$_SESSION['uid']} order by DateUploaded DESC";
$result=mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc($result)){
    $data[] = $row;
}
mysql_close($conn);
// end of the business logic

// the following is anything to do with presentation
if($_SESSION['uid'] == 1){
    // width='610' Detail 1 - Detail 5
    $width = 610;
    $details = array(1=>'Detail 1','Detail 2','Detail 3','Detail 4','Detail 5');
} else {
    // width='270' Detail (1) only
    $width = 270;
    $details = array(1=>'Detail');
}

// the html document (template) follows -
?>
<body>
<table width="680" border="0" align="center" cellpadding="4" cellspacing="0">
<tr><td> </td></tr>
<tr><td class="text-heading"><p>Commission Statements</p></td></tr>
<tr><td>
<table width="<?php echo $width; ?>" border="0" align="center" cellpadding="4" cellspacing="0">
<tr class="text-sectionheading">
<td width="100">Date </td>
<td width="85">RCTI</td>
<?php
foreach($details as $legend){
    echo "<td width='85'>$legend</td>";
}
echo "</tr>";

foreach($data as $test){
    $id = $test['id'];
    echo"<tr>";
    echo"<td class='text-questions'>{$test['DateUploaded']}</td>";
    echo"<td><a class='{$test['RCTIcssclass']}' href ='{$test['RCTIURL']}'>{$test['RCTIImageType']}</a></td>";
    foreach($details as $key=>$not_used){
        $class = $test["Detail{$key}cssclass"];
        $url = $test["Detail{$key}URL"];
        $imgtype = $test["Detail{$key}ImageType"];
        echo"<td><a class='{$class}' href ='{$url}'>{$imgtype}</a></td>";
    }
    echo "</tr>";
}
?>
</table>
</td></tr>
</table>
</body>
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.