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. -->
Related
This question already has answers here:
Checking if mysqli_query returned any values?
(2 answers)
Closed 3 months ago.
I'm using while loop to fetch data from the database I want to display like nothing found the result is null or empty. I've tried using if/else with like: if (empty($jobs)) echo 'nothing found'; that doesn't work. if(!mysqli_fetch_assoc($jobs)) works but if don't show the jobs if available.
Loop
<?php while ($job = mysqli_fetch_assoc($jobs)) { ?>
<tr class="custom-table-body-titles">
<td><?php echo h($job['updated_at']); ?></td>
<td>
<?php echo h($job['title']); ?>
</td>
<td>0/<?php echo h($job['required_freelancers']); ?></td>
<td><?php echo h($job['delivery_time']); ?> Days</td>
<td>$<?php echo h($job['budget']); ?></td>
<td>
Apply
</td>
</tr>
<?php } ?>
You have to do a validation to check if any results were found before even starting the loop. Assuming $jobs is an instance of mysqli_result returned by mysqli_query().
if (mysqli_num_rows($jobs) > 0)
{
// your while loop
}
else
{
echo "no jobs found";
}
You should keep PHP and HTML separate as much as you can.
You can fetch all results into an array before and then foreach on that array.
$jobs = $jobs->fetch_all(MYSQLI_ASSOC);
if($jobs) {
foreach ($jobs as $job) {
}
} else {
}
A good question which is sadly most of time is answered incorrectly.
It's a very bad practice to mix the database-related code and HTML code. There should never be a while loop like this.
Instead, the data should be fetched into array,
$jobs = [];
while ($row = mysqli_fetch_assoc($result)) {
$jobs[] = $row;
}
which is then used to output the data. And once you have this array, you can use it to tell whether the query returned any data or not:
<?php if ($jobs) { ?>
<?php foreach ($jobs as $job) { ?>
<tr class="custom-table-body-titles">
<td><?php echo h($job['updated_at']); ?></td>
<td>
<?php echo h($job['title']); ?>
</td>
<td>0/<?php echo h($job['required_freelancers']); ?></td>
<td><?php echo h($job['delivery_time']); ?> Days</td>
<td>$<?php echo h($job['budget']); ?></td>
<td>
Apply
</td>
</tr>
<?php } ?>
<?php } else {?>
no data
<?php } ?>
I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }
Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.
I want to prevent amaun_caj , amaun_pelbagai , amaun_penalti , amaun_tunggakan from being repeated so if the id_akaun is the same the data will not appear, my code only works on amaun_caj, and for the rest, not a single data appear, what's the problem?
<?php
$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $jenis; ?>
</td>
<td>
<?php echo $id_akaun;
?>
</td>
<td>
<?php echo $no_telefon; ?>
</td>
<td>
<?php echo $lokasi; ?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_caj;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_pelbagai;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_penalti;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_tunggakan;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $jumlah_bayaran;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<?php
}
?>
Using temporary variable $old_id_akaun might not be the best practice. If you still want to do it like that, i suggest you user ORDER BY id_akaun in your SQL syntax.
In my oppinion, you might get rid of temporary variable and follow these steps,
1. Create empty array outside your while loop. For the sake of easy
understanding, let's called it $list_id_akaun.
2. Inside your while loop, after you get $id_akaun, check whether $id_akaun
is inside $list_id_akaun
3. If not exists, insert it to $list_id_akaun and continue echoing your
table row
4. If exists, skip to the next row.
I don't know why you don't want to use a select distinct clause in this case, but just put them inside a container then check inside the loop:
<?php
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)):
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $jenis; ?></td>
<td><?php echo $id_akaun; ?></td>
<td><?php echo $no_telefon; ?></td>
<td><?php echo $lokasi; ?></td>
<?php if(!in_array($id_akaun, $temp)): ?>
<td><?php echo $amaun_penalti; ?></td>
<td><?php echo $amaun_tunggakan; ?></td>
<td><?php echo $jumlah_bayaran; ?></td>
<?php $temp[] = $id_akaun; ?>
<?php else : ?>
<td></td><td></td><td></td>
<?php endif; ?>
</tr>
<?php endhile; ?>
I have a php table that displays the days of the month in a table:
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
I have a second table that calls a separate query for each day of the month:
<tr>
<td> </td>
<td> </td>
<td><?php do { ?>
<?php echo $row_apr1['appt_time']; ?> <em><?php echo $row_apr1['nickname']; ?></em><br>
<?php } while ($row_apr1 = mysql_fetch_assoc($apr1)); ?></td>
<td><?php do { ?>
<?php echo $row_apr2['appt_time']; ?> <em><?php echo $row_apr2['nickname']; ?></em><br>
<?php } while ($row_apr2 = mysql_fetch_assoc($apr2)); ?></td>
<td><?php do { ?>
<?php echo $row_apr3['appt_time']; ?> <em><?php echo $row_apr3['nickname']; ?></em><br>
<?php } while ($row_apr3 = mysql_fetch_assoc($apr3)); ?></td>
<td><?php do { ?>
<?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?></td>
<td><?php do { ?>
<?php echo $row_apr5['appt_time']; ?> <em><?php echo $row_apr5['nickname']; ?></em><br>
<?php } while ($row_apr5 = mysql_fetch_assoc($apr5)); ?></td>
</tr>
Is there a way I can merge the code together into one table, like using the $day_num value to call the query code?
Example: April 4th - $day_num would be = 4 and display a 4 in the table. But then can't I use php concatenation or something to call the code:
<br><?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?>
Sorry if this has been done before a million times. I've been searching and can't seem to find what I'm looking for.
Thanks!
You can use the variable variables or $$ option. http://www.php.net/manual/en/language.variables.variable.php
<td>
<?php
$variable_name = 'apr' . $day_num;
do { ?>
<?php echo $row['appt_time']; ?>
<em><?php echo $row['nickname']; ?></em><br>
<?php } while ($row = mysql_fetch_assoc($$variable_name)); ?>
</td>
Not sure this is the best option for you but based off the code you have supplied it is a possible solution.
FYI you shouldn't use mysql_ functions as they are deprecated.