WAMFT1 Posted February 11, 2014 Share Posted February 11, 2014 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) Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/ Share on other sites More sharing options...
requinix Posted February 11, 2014 Share Posted February 11, 2014 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 } Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468433 Share on other sites More sharing options...
WAMFT1 Posted February 11, 2014 Author Share Posted February 11, 2014 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468437 Share on other sites More sharing options...
requinix Posted February 11, 2014 Share Posted February 11, 2014 (edited) 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 February 11, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468440 Share on other sites More sharing options...
WAMFT1 Posted February 11, 2014 Author Share Posted February 11, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468441 Share on other sites More sharing options...
mac_gyver Posted February 11, 2014 Share Posted February 11, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468444 Share on other sites More sharing options...
Solution mac_gyver Posted February 11, 2014 Solution Share Posted February 11, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468445 Share on other sites More sharing options...
WAMFT1 Posted February 11, 2014 Author Share Posted February 11, 2014 Thanks mac_gyver, it works exactly how I wanted it to. I will put in some test data later and test it out but looks awesome. Quote Link to comment https://forums.phpfreaks.com/topic/286098-echo-table-if-field-x/#findComment-1468447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.