Multiple values in database field - php

I have a specifications table for some products. Some products come in different colors. So to make that clear it shows some little color boxes.
So I have table field colors which stores colors like this:
black,white,green
I came this far with the code:
<?php
foreach($result as $kleur) {
$myString = $kleur['kleur'];
$myArray = explode(',', $myString);
print_r($myArray);
?>
<tr>
<td width="20%">Kleur:</td>
<td>
<div class="item">
<img src="http://www.ledframes.nl/images/product_details/accessoires/kleuren/<?php echo $myArray[0];?>.jpg"/></a>
</div>
</td>
</tr>
<?php
}
?>
The print_r shows all the values from the field. And if the change the number at echo $myArray[0] it shows the right color boxes in my specifications table.
Now my question is, if the color field has multiple values how do I loop it so it shows all the color blocks?
I read somewhere that you can use something like this:
foreach( $array as $name => $key)
I have tried to implement this to my code but can't get it to work

If I understand your problem correctly, $result is the array of database records, and for each record (which you call $kleur), $myArray is the array of color names.
So, to show all the color fields, you need another loop over $myArray, e.g. like this:
<?php
foreach($result as $kleur) {
$myString = $kleur['kleur'];
$myArray = explode(',', $myString);
print_r($myArray);
?>
<tr>
<td width="20%">Kleur:</td>
<td>
<?php
foreach($myArray as $colorName) {
?>
<div class="item">
<img src="http://www.ledframes.nl/images/product_details/accessoires/kleuren/<?php echo $colorName;?>.jpg"/></a>
</div>
<?php
}
?>
</td>
</tr>
<?php
}
?>

Related

No foreach Display

<?php
$pros_array_id= array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro):
if($pro['paint_id'] == $pros_array_id):
?>
<table>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>" >
</td>
</tr>
</table>
<?php
endif;
endforeach;
?>
Am trying to display session cart items and its not showing anything. When I print_r($productee) and print_r($pros_array_id) after the foreach statement both display the accurate data, yet nothing displat in the <tr> tag.
The is to display the result
When I implode $pros_array_id like this "$imp = implode(" ",$pros_array_id);" and put the variable in the if-statement, it works fine if only one product is in the session, but the moment I add more than one products in the session, nothing is display again.
Please can someone point to me what I should do?
Thanks
Try it like this:
<table>
<?php
$pros_array_id = array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro) {
//Also see and try to disable this check to see if it works then!
if($pro['paint_id'] == $pros_array_id) { ?>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>">
</td>
</tr>
<?php
}
}
?>
</table>
Always make sure while debugging this kind of code to eliminate any checks that could result in false, so try disabling the "if clause" as well, to see if it displays something then.

displaying table field names dynamically with cake php

I am trying to display a list of all entries in the table people. I have the list of entries generating correctly as a html table but I want to ad a row at the top of the table with the table field names. I could do this manually but figure there must be a way to do it dynamically in case I add or remove fields later on.
here is the controller function
public function peopleDisplay() {
$this->set('people', $this->people->find('all'));
}
Are the field names already in the array that generates? If so how do I reference them? If not how do I get them to be?
Here is the view so far
<table>
<tr>
***were I want the row of field names to go***
</tr>
<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
The column names are the keys in your array. For instance, 'id', 'firstName', and 'secondName' are the column names.
Another way to approach would be to get the column names as a separate array in your controller and then output them in your view.
$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));
Then in your view:
foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }

How can I tell if the current result is the last result in PHP

I've got a new online store written in PHP and MySQL.
<div class="content-area">
<div class="page-heading">
<h1>Store</h1>
</div>
<p style="padding-top: 5px;"><strong>You are here:</strong> Home » Store</p>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<?php
$categories=mysql_query("SELECT * FROM categories WHERE parent='0' ORDER by owner ASC, title ASC");
while($categoriesRow=mysql_fetch_array($categories)) {
$categoriesSub=mysql_query("SELECT * FROM categories WHERE parent='$categoriesRow[id]'");
?>
<tr>
<td valign="top">
<div class="product_list">
<div class="image_product">
<img alt="<?php echo $categoriesRow['title']; ?>" src="<?php echo $cls->truska(true); ?>/theme_section_image.gif" border="0" style="vertical-align: middle;" />
</div>
<div>
<h3 class="product"><?php echo $categoriesRow['title']; ?> <?php if(mysql_num_rows($categoriesSub) > 0) { ?>(<?php while($categoriesSubRow=mysql_fetch_array($categoriesSub)) { }?>)<?php } ?></h3>
</div>
</div>
</td>
</tr>
<tr>
<td class="dotted_line_blue" colspan="1">
<img src="<?php echo $cls->truska(true); ?>/theme_shim.gif" height="1" width="1" alt=" " />
</td>
</tr>
<?php
}
?>
</table>
</div>
Where my second While loop is, I need to work out what is the last result, so I can omit a comma from my while loop.
It will show as 'Lego (Lego City, Lego Starwars,)' but I want it to show as 'Lego (Lego City, Lego Starwars)'.
How can I get if the current result is the last?
You can fix this by coming at it from the other direction.
Instead of appending a comma after each result except the last, try pre-pending a comma on every result except the first.
Set up a variable called $first outside your loop, and set it to 1. Inside the loop:
if ($first == 0) {
echo ",";
} else {
$first = 0;
}
don't add the comma if it is the first result, and add it before in all the next ones.
Just build your array of results and implode it. This takes care of any counting automatically:
$comma_separated = implode(",", $array);
You shouldn't use plain mysql access, look at PDO.
Answering your question, try something like this:
$items = array();
while ($row = mysql_fetch_assoc($result)) {
$items[] = $row['foo'];
}
echo implode(', ', $items);

Dynamic data population in table form with Code Igniter

I have a pretty simple question, but for some reason I am drawing a blank. I have the following code in my view file, and I want to display the results in a two column table, so the first entry would be on the left, the next would be on the right then the next one after that would be below the first row, and eventually I will use the pagination class (haven’t gotten that far yet) For some reason I can not figure out how to get the results to display in a 2 column format… only one. Any help would be greatly appreciated.
Ideally I would like to have 4 columns, but the code below was started with just the idea of 2 columns.
Thanks!
<table>
db->query($sql);
foreach ($query->result() as $row)
{
echo("");
echo("");
echo $row->Title;
echo ("<br/>");
?>
<img name="<?php echo $row->Thumb;?>" src="../uploaded/portfolio/thumbs/<?php echo $row->Thumb;?>" alt="">
<?php
echo("<br/>");
echo $row->DescText;
echo("</td>");
echo("<td>");
// Display next picture here
echo("</td>");
echo("</tr>");
}
?>
../
Your code example is rather confusing, but I think from your description that you're trying to do something like this:
<table>
<tr>
<?php $i = 0; foreach($query->result() as $row): ?>
<?php if ($i % 2 == 0): ?>
</tr><tr>
<?php endif; ?>
<td>
<?php //whatever you want to put in your column goes here; ?>
</td>
<?php $i++; endforeach; ?>
</tr>
</table>
If you want the table to be four rows across, just change the "if ($i % 2 == 0)" to "if ($i % 4 == 0)".

Basic php form help (2)

This is an extension of a question I had yesterday.
I am trying to make a little php calculator that will show how much people can save on their phone bills if they switch to VoIP, and how much they can save with each service.
I have a form that will spit out the right amount for a monthly bill here:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
But now I need to integrate that with some other data and put in a table. The prices for each of the different services are in another file called "values.php". The values are:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value.
The php code currently looks like this:
<?php $monthlybill = $_GET['monthlybill']; ?>
Your monthly bill was <?php echo "$monthlybill"; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span><br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div> </td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
I want each of the seven rows of the
table to have one of the
offercalcsavings values subtracted
from the monthlybill value.
You need to do the math somewhere. Presumably you would do this before the echo statement where you output your row.
$offerwithsavings = $monthlybill - $offercalcsavings
Then just be sure to include it in your table somewhere.
<td >'.$offerwithsavings.'</td>
The real prescription for this code is an array and foreach() loop, but I thought I'd keep my answer simple for now. Arrays and foreach() loops are very powerful and relatively quick and easy to learn. You would do well to give them some in depth study.
You can include the values.php file like this:
include 'path/to/values.php';
If you put the values in values.php in an array you can easily loop through them using a foreach loop:
in values.php;
$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;
Then in the other file:
include 'path/to/values.php';
foreach($values as $name => $amount){
echo '<some_markup>';
echo "$name $amount";
echo '</some_markup>';
}
Egads.
Use arrays!
So:
$offercalcsavings[0] = "24.99";
$offercalcsavings[1] = "20.00";
etc. etc.
Then, you're looping the output really:
for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
echo "<html bits>";
echo $offercalcsavings[$i];
echo $offerlink[$i];
etc. etc.
}

Categories