<?php
if(arg(0)=="test"){
echo "code here";
}else{
echo '<div class="item-list">
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<<?php print $options['type']; ?>>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</<?php print $options['type']; ?>>
</div>
';}
the else echo is too long. is there a way to make it small.
Generally speaking, this is bad code. You are messing business logic with views. I would definitely use a very simple template system like :
Simple and Fast Template Engine
This code cannot be maintained and if it gets bigger, it would be a hell to even read. Please do yourself a favor and separate logic from views.
Take a look at this nice example from the url above, to see how code should be organized :
<?php
require_once('template.php');
$tpl = & new Template('./templates/');
$tpl->set('title', 'User Profile');
$profile = array(
'name' => 'Frank',
'email' => 'frank#bob.com',
'password' => 'ultra_secret'
);
$tpl->set_vars($profile);
echo $tpl->fetch('profile.tpl.php');
?>
The associated template looks like this:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr>
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr>
<td>Password</td>
<td><?=$password;?></td>
</tr>
</table>
And the parsed output is as follows:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td>Frank</td>
</tr>
<tr>
<td>Email</td>
<td>frank#bob.com</td>
</tr>
<tr>
<td>Password</td>
<td>ultra_secret</td>
</tr>
</table>
I'm surprised that code would even run. Here's a better way, without using echo: just close the php interpreter while you generate your html output.
<?php
if(arg(0)=="test"){
echo "code here";
}else{
?>
<div class="item-list">
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<<?php print $options['type']; ?>>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</<?php print $options['type']; ?>>
</div>
<?
}
?>
<?php
$output = "<div class='item-list'>";
if(!empty($title)) $output .= "<h3>".$title."</h3>";
$output .= $options['type'];
foreach($rows as $id => $row) {
$output .= '<li class="' . $classes[$id] . '">' . $row . '</li>';
}
$ouput .= $options['type'] . "</div>";
$output = (arg(0) == "test" ? "code here" : $output);
echo $output;
?>
It looks nicer if you don't switch between PHP and HTML context (also indentation does help):
<?php
if (arg(0)=="test") {
echo "code here";
}
else {
echo '<div class="item-list">'
. #$title
. "<$options[type]>";
foreach ($rows as $id => $row) {
print "<li class=\"{$classes[$id]}\">$row</li>";
}
echo "</$options[type]>";
}
Related
I need hide first one < td > <?php echo $user_feature['tab_title_single']; ?> if second < td > are abolutely empty without any checked ('tab_title_labels') values.
<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
if (!empty($id)) {
$post_id = $id;
}
$user_features = array(
array(
'tab_title_single' => 'Apsauga',
'tab_title_labels' => 'Imobilaizeris,Signalizacija,Palydovinė sekimo sistema,Šarvuotas (apsaugos)'
),
array(
'tab_title_single' => 'Audio/video įranga',
'tab_title_labels' => 'CD grotuvas,MP3 grotuvas,Papildoma audio įranga,CD keitiklis,AUX jungtis,Žemų dažnių garsiakalbis,DVD grotuvas,USB jungtis,Laisvų rankų įranga,Apple CarPlay / Android Auto'
),
array(
'tab_title_single' => 'Eksterjeras',
'tab_title_labels' => 'Lengvojo lydinio ratlankiai,LED dienos žibintai,LED žibintai,Žibintai „Xenon“,Rūko žibintai,Kablys,Priekinių žibintų plovimo įtaisas,Stogo bagažinės laikikliai,Automatiškai užsilenkiantys veidrodėliai,Žieminių padangų komplektas'
),
array(
'tab_title_single' => 'Elektronika',
'tab_title_labels' => 'El. valdomi veidrodėliai,El. valdomas bagažinės dangtis,Automatiškai įsijungiantys žibintai,Borto kompiuteris,El. reguliuojama vairo padėtis,Kritulių jutiklis,Šildomi veidrodėliai,Atstumo jutiklių sistema,Beraktė sistema,Autopilotas,El. šildomas priekinis stiklas,Start-Stop funkcija,Valdymas balsu,Pavarų perjungimas prie vairo,LCD ekranas,Navigacija/GPS'
)
);
?>
<?php if (!empty($user_features)) {
if (!empty($post_id)) {
$features_car = get_post_meta($post_id, 'additional_features', true);
$features_car = explode(',', $features_car);
} else {
$features_car = array();
}
foreach ($user_features as $user_feature) { ?>
<table style="width: 100%; margin-bottom: 0px">
<tr>
<td style="width: 16%; padding-left:5px;">
<div class="heading-font" style="color:#555555;font-size: 13px;font-weight: 500;"><?php echo $user_feature['tab_title_single']; ?></div>
</td>
<td style="width: 84%; padding-left:5px;">
<?php $features = explode(',', $user_feature['tab_title_labels']); ?>
<?php if (!empty($features)): ?>
<?php foreach ($features as $feature): ?>
<?php
$checked = '';
$hide = 'style="display:none;"';
if (in_array($feature, $features_car)) {
$checked = 'checked';
$hide = '';
};
?>
<label <?php echo $hide; ?>>
<span class="featuresspan"><?php echo esc_attr($feature); ?></span>
</label>
<?php endforeach; ?>
<?php endif; ?>
</td>
</tr>
</table>
<?php }
}
?>
To compare two arrays ($features and $features_car) in php, the most direct tool for that task is array_intersect(). Although I should tell you (and researchers) that it would be far, far better to Normalize your table data so that you are not storing comma-separated values. Having a normalized/granular table of labels and their individual features spread across multiple rows will allow your application to enjoy cleaner and more efficient queries and allow you to move the filtering processes from php to mysql where it belongs.
Code: (Demo)
$features_car = ['USB jungtis', 'Rūko žibintai'];
foreach ($user_features as $user_feature) {
$features = explode(',', $user_feature['tab_title_labels']);
$matched_features = array_intersect($features, $features_car);
if ($matched_features) {
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<div class=\"heading-font\">{$user_feature['tab_title_single']}</div>";
echo "</td>";
echo "<td>";
foreach ($matched_features as $show_feature) {
echo "<label><span class=\"featuresspan\">{$show_feature}</span></label>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
Output:
<table>
<tr>
<td>
<div class="heading-font">Audio/video įranga</div>
</td>
<td>
<label><span class="featuresspan">USB jungtis</span></label>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="heading-font">Eksterjeras</div>
</td>
<td>
<label><span class="featuresspan">Rūko žibintai</span></label>
</td>
</tr>
</table>
p.s. I don't know if I support the stacking of <table> elements like this, but it is up to you how you wish to style your markup. You should move ALL of your inline styles to an external stylesheet so that your markup is easier to read and manage. I removed the esc_attr() only so that my demo would work without errors -- you can safely apply it to all variables that you are concerned about.
I've gotten into trouble while working on a school project, and I'm stuck.
I'm making a website where you can easily search for amusement parks.
I made a while loop with PHP and put information from the DB into the table which I looped. I've put all the information in a TD and I want to create a-href so when I click on one of the first parks it will redirect to an example: example.php?park=1 for the second park example: example.php?park=2.
Here is the while:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
Each pretpark has an ID, the DB table of it is called ParkId.
Can someone help me where I have to put the a href. And how I can do it like its like this example: example.php?park= ParkId
I hope everything is understandable what I just said :/
Thanks,
You can do like this:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "<td><a href='example.php?park=".$pretpark['Parkid']."'> More Details</a></td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
**echo "<td><a href='detailpage.php?id=".$pretpark['ParkId']."'></td>";**
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
I have added a line with bold formatting. You can get clue from this.
instead of the line
echo "<td>".$pretpark['ParkName']."</td>";
it would be something like..
echo "<td>".$pretpark['ParkName']."</td>";
backslashes are needed to escape the quotes so they come out in the HTML and are not interpreted by php
I am trying to get some custom attributes to show in magento like Color, Delivery_Time...
I could call Some Attributes with :
<?php echo $item->getName();?>
<?php echo $item->getWeight();?>
But i could'nt call the most of the attributes. i've tried also :
<?php echo $this->htmlEscape($item->getData('luftkammern'));?>
<?php echo $item->getAttributeText('spannung'); ?>
Nothing works!!
Code of attributes :
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product,$_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
code
echo $this->__('Additional Information');?>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
can anyone help please ?
If you need to get those attributes in checkout page, you can do like this :
$_product = $item->getProduct();
$pid = $_product->getId();
$product = Mage::getModel('catalog/product')->load($pid);
/* getting some attributes */
$color = $product->getData('color');
$manufacturer = $product->getData('manufacturer');
$delivery_date = $product->getData('delivery_date');
....
Hope it will help you.
You can get attributes from a product by calling the correct get function.
For example to receive the color use:
echo $item->getColor();
To receive the manufacturer use:
echo $item->getManufacturer();
Also you have to make sure that your product has been fully loaded. Try this:
print_r($item->getData());
Please check the following code:
$attributes = $product->getAttributes();
$additional_data = array();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined()) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode())) {
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') {
$value = Mage::helper('catalog')->__('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$additional_data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
/* Your code here */
<h2><?php echo $this->__('Additional Information') ?></h2>
...
<?php foreach ($additional_data as $_data): ?>
...
I have been on this for a day now and still cant solve it though it should be quite simple. I have a php code.
foreach($cart as $line=>$item)
{
echo form_open("sales/edit_item/$line");
?>
<td style="align:center;"><?php echo $item['name']; ?></td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
</div></div>
<td><?php echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></td>
<?php
if($item['allow_alt_description']==1)
{
}
else
{
if ($item['description']!='')
{
}
else
{
}
}
?>
</td>
<td> </td>
<td style="color:#2F4F4F";>
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
<td colspan=3 style="text-align:left;">
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
</tr>
<tr style="height:3px">
<td colspan=8 style="background-color:white"> </td>
</tr> </form>
<?php
}
}
?>
This creates fields with relevant data and works ok. I just need to get the last field and echo it, I have tried everything and it still loops through the array, or does not work. This might be simple to someone else but it has confused me for a day now.
When you use a loop, such as for or foreach or while, it will iterate over every single child element of the array until it reaches the end. You don't need to loop, you simply need to access the last member of the array, like so:
$lastLine = end( array_keys($cart) );
echo form_open("sales/edit_item/{$lastLine}");
Edit: Now that I understand a bit better:
$lastItem = array_slice($cart, -1, null, true);
$line = key($lastItem);
$item = reset($lastItem);
echo form_open("sales/edit_item/{$line}");
?>
<!-- do all your html-ish stuff here. -->
I'm trying to create a table in php that would show the data on the mysql database based on the check box that is checked by the user.
As you can see in this screen shot, it will have problems when you did not check on a checkbox before the one that will be the last:
http://www.mypicx.com/04282010/1/
Here is my code:
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName )
echo "<th>LASTNAME</th>" ?>
<?php if ( $ShowFirstName )
echo "<th>FIRSTNAME</th>" ?>
<?php if ( $ShowMidName )
echo "<th>MIDNAME</th>" ?>
<?php if ( $ShowAddress )
echo "<th>ADDRESS</th>" ?>
<?php if ( $ShowGender )
echo "<th>GENDER</th>" ?>
<?php if ( $ShowReligion )
echo "<th>RELIGION</th>" ?>
<?php if ( $ShowBday )
echo "<th>BIRTHDAY</th>" ?>
<?php if ( $ShowContact )
echo "<th>CONTACT</th>" ?>
</tr>
<?php
while($row = mysql_fetch_array($result2))
{?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<td><?php
if ( $ShowLastName )
echo $row['LASTNAME'] ?></td>
<td><?php
if ( $ShowFirstName )
echo $row['FIRSTNAME'] ?></td>
<td><?php
if ( $ShowMidName )
echo $row['MI'] ?></td>
<td><?php
if ( $ShowAddress )
echo $row['ADDRESS'] ?></td>
<td><?php
if ( $ShowGender )
echo $row['GENDER'] ?></td>
<td><?php
if ( $ShowReligion )
echo $row['RELIGION'] ?></td>
<td><?php
if ( $ShowBday )
echo $row['BIRTHDAY'] ?></td>
<td><?php
if ( $ShowContact )
echo $row['S_CONTACTNUM'] ?></td>
</tr>
<?PHP } ?>
</table>
<?PHP }
mysql_close($con);
?>
What can you recommend so that the output will not look like this when you one of the checkbox before a checkbox is not clicked: http://www.mypicx.com/04282010/2/
instead of
<td><?php
if ( $ShowGender )
echo $row['GENDER'] ?>
</td>
you should do something like
<?php
if ( $ShowGender )
echo "<td>".$row['GENDER']."</td>" ?>
So that the <td> tags only appears if the "if" statement is true.
Ok first thing's first, let's clean your code up, because it's so difficult to read in it's current format:
<?php
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
<?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
<?php if ( $ShowMidName ) { ?><th>MIDNAME</th><?php } ?>
<?php if ( $ShowAddress ) { ?><th>ADDRESS</th><?php } ?>
<?php if ( $ShowGender ) { ?><th>GENDER</th><?php } ?>
<?php if ( $ShowReligion ) { ?><th>RELIGION</th><?php } ?>
<?php if ( $ShowBday ) { ?><th>BIRTHDAY</th><?php } ?>
<?php if ( $ShowContact ) { ?><th>CONTACT</th><?php } ?>
</tr>
<?php while($row = mysql_fetch_array($result2)) {?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
<?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>
<?php if ( $ShowMidName ) { echo('<td>'.$row['MI'].'</td>'); } ?>
<?php if ( $ShowAddress ) { echo('<td>'.$row['ADDRESS'].'</td>'); } ?>
<?php if ( $ShowGender ) { echo('<td>'.$row['GENDER'].'</td>'); } ?>
<?php if ( $ShowReligion ) { echo('<td>'.$row['RELIGION'].'</td>'); }?>
<?php if ( $ShowBday ) { echo('<td>'.$row['BIRTHDAY'].'</td>'); }?>
<?php if ( $ShowContact ) { echo('<td>'.$row['S_CONTACTNUM'].'</td>'); }?>
</tr>
<?php } ?>
</table>
<?php }
mysql_close($con);
?>
Your best bet would be to try putting this code in and telling us if this improves things?
EDIT
Ah, as the others have said your <td> tags are sitting outside of your condition, still, the above code is much easier to read and will help future debugging :-)
You only print table header elements (<th>) if the corresponding $isField variable is set, but you print all table cells, only testing whether or not to print the cell contents.
Instead of all that, loop over the fields to be printed out. No need to test each and every field.
Example form:
<form action="..." method="POST">
<h4>Student Information</h4>
<?php foreach ($studentFields as $key => $label) { ?>
<input type="checkbox" name="show[<?php echo $key; ?>]" id="show_<?php echo $key; ?>"/><label for="show_<?php echo $key; ?>"><?php echo $label; ?></label>
<?php } ?>
<h4>Parent Information</h4>
<?php foreach ($parentFields as $key => $label) { ?>
<input type="checkbox" name="show[<?php echo $key; ?>]" id="show_<?php echo $key; ?>"/><label for="show_<?php echo $key; ?>"><?php echo $label; ?></label>
<?php } ?>
</form>
Form handler:
<table>
<thead><tr>
<?php foreach ($fields as $key => $label) { ?>
<th><?php echo $label; ?></th>
<?php } ?>
</tr></thead>
<tbody>
<?php foreach ($results as $row) { ?>
<tr>
<?php foreach ($fields as $key => $label) { ?>
<td><?php echo $row[$key]; ?></td>
<?php } ?>
</tr>
<?php ?>
</tbody>
The foreach ($results as $row) { needs to be rewritten as a while loop if you stick with the outdated mysql driver, but works with PDOStatement. Switching to PDO also makes it easier to injection vulnerabilities, as prepared statement parameters are invulnerable to them. You can also rewrite that SELECT * to only fetch the requested columns, reducing DB load.
$validFields = array('last' => 'Last Name', 'first' => 'First Name', 'stAddr' => 'Address', ...);
$fields = array_intersect($validFields, $_POST['show']);
You could even make it self-configuring by constructing the $validFields array by inspecting the DB table(s), though this would incur an extra table query.
Yeah, do it the same way as you've done the th tags, with the if statement around the td tags, rather than inside them. Way you've done it now will always show 9 columns, no matter what check boxes are selected.
Your 're printing the cells in the iteration, but only it's content depends on the condition.
<?php
if ( $ShowContact )
echo '<td>' . $row['S_CONTACTNUM'] . '</td>' ?>