Table row beside each other - php

I tried to allow the last td in this code to be beside the previous td, but I can't and the td is printed in a new line. How to allow them to be beside each other,the problem is that the 1st 5 td are in a foreach loop and the last td is not follow this foreach as it's value is a function and not a key or a value in the foreach.
<?php foreach($downloads as $dl) { ?>
<tr id="this">
<td ><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td>
<a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>">
<?=$dl['title']?>
</a>
</td>
<td>
<center>
<?=$dl['sname']?>
</center>
</td>
<td align="center"><?=$dl['views']?></td>
</tr>
<?php } ?>
<td align="center"><?=$core->use_love(); ?></td>
The function of the last td
public function use_love(){
$sql=mysql_query("select * from wcddl_downloads ORDER BY id DESC LIMIT ".$this->pg.",".$this->limit."");
while($row=mysql_fetch_array($sql))
{
$down_id=$row['id'];
$love=$row['love'];
?>
<div class="box" align="center">
<a href="#" class="love" id="<?php echo $down_id; ?>">
<span class="on_img" align="left"> <?php echo $love; ?> </span>
</a>
</div>
<?
}
}

The last <td> (the one outside the foreach loop) is on a new line because it's outside the last <tr> tag. One way to solve this is to always close the </tr> tag after the last <td>, like this:
<?php
$first_time = True;
foreach($downloads as $dl) {
// If this is the first time through the loop, don't echo a </tr> tag:
if ($first_time) {
$first_time = False;
} else {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"><?=$dl['views']?></td>
<?php
}
?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>
This will always put the last <td> in the final row.

UPDATE: I just saw you added a diagram. This answer now makes no sense as the text description given earlier has nothing to do with what you want to achieve in the diagram.
I submit the modification to Steve Nay's answer as you need the same number of TD in all your TR. When you don't have the same count, you need to use colspan to achieve it. I've added a counter to check if it's the last time you loop. Here it goes:
<?php
$downloads_count = count($downloads);
$counter = 0;
foreach($downloads as $dl) :
$counter++;
// If this is the first time through the loop, don't echo a </tr> tag:
if ($counter > 1) {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"<?php if ($downloads_count != $counter) echo ' colspan="2"'; ?>><?=$dl['views']?></td>
<?php endforeach; ?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>

Related

Create a Counter in a ranking table

I have a table of articles that are arranged by the number of visits, where the most visited one is on top, i.e it has Rank of 1. I tried adding a Rank column in the table, but can't get it to work. The Rank column does not exist in the Database, so this should be an incremental counter that starts with 1 and ends with the number of articles listed in the table.
Here's my code, which does not work
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php foreach($popular_articles as $article) {
//create array for most popular
?>
//set counter variable
$counter = 1;
while($row = mysql_fetch_array($result))
<tr>
echo "<td style="width:10%">".$counter."</td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php if($article->art_hit_count){ echo '<b>'.$article->art_hit_count.'</b>'; }else { echo '<b>0</b>'; } ?> times.</td>
</tr>
$counter++; //increment counter by 1 on every pass
echo <?php } ?>
</table>
Increasing the counter must be done in PHP. Currently the line that does that is in the HTML:
$counter++; //increment counter by 1 on every pass
I also removed the while, since it didn't seem to do anything, as if it was accidentally pasted there.
The loop should look like this:
<?php
foreach($popular_articles as $article) {
//set counter variable
$counter = 1;
<tr>
<td style="width:10%"><?php echo $counter; ?></td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php
if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
}else {
echo '<b>0</b>';
} ?> times.</td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} ?>
Alternatively, you can display the counter and update it in one go:
<td style="width:10%"><?php echo $counter++; ?></td>";
...
<?php
// $counter++; // You won't be needing this anymore.
} ?>
I think you missed to initialize your counter before the loop (in the commentaries, I can see a $counter = 1, but it's trully executed.
And at the end, you try to increment a variable but you're not in a PHP script, so it can not work. Try my corrected version to see if it work.
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php
$counter = 1;
foreach($popular_articles as $article) {
//create array for most popular
?>
<tr>
<td style="width:10%"> <?php echo $counter; ?></td>;
<td style="width:50%"> <?php echo $article->art_title; ?></td>
<td style="width:20%"> <?php echo $article->art_type; ?></td>
<td style="width:20%">
<?php if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
} else {
echo '<b>0</b>';
} ?> times. </td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} //the end of FOR loop ?>
</table>
EDIT
If you want to make a link for the art_title. Just replace this line :
<td style="width:50%"> <?php echo $article->art_title; ?></td>
By this one :
<td style="width:50%"> <?php echo $article->art_title; ?></td>

How do you increment a number with every table row?

It is very simple, but I cannot do it. Now it takes every information from my database and introduces it in the Nr. td. How can I increment the value with every row?
<?php while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td (this is what I want to auto-increment)>
<?php
$i = 1;
foreach($row as $i){
$i++;
echo $i;
}
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
Thanks!
Try this:
Modify your code as given below:
<tbody>
<?php
$i = 1;
while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php
echo $i;
$i++;
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php }?>
</tbody>
Initialize $i before the while loop then echo in the td then increment it.
<?php
$i=1;
while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td width="5">
<?php
echo $i;
$i++;
//You can join the above 2 lines into 1 like this:
// echo $i++;
// This way you're telling php, echo $i then increment it by 1
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
NB: if you're just learning PHP, you shouldn't be learning mysql_* as they are deprecated and will be removed in future releases. Check out mysqli or PDO.
initialize
$i = 1;
outside while loop
while ($row=mysql_fetch_array($query))
, so that it will increase.
Have your row counter ($rowNum) outside of the while
<?php
$rowNum = 1; ?>
<tbody>
<?php while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php echo $rowNum; $rowNum++ ?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php } ?>
</tbody>

Last loop in CodeIgniter foreach

I'm trying to catch the last loop in my CodeIgniter for-loop. I'm trying to generate a < hr /> under every news item but the last one. This is my code:
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><hr /></td>
</tr>
<?php endforeach; ?>
</table>
Уou can try something like this:
<?php for($i = 0, $lastIDX = count($news)-1; $i<=$lastIDX; $i++): ?>
<!-- html code ... $news_item is $news[$i] now -->
<? if ($i !== $lastIDX) : ?>
<tr>
<td><hr /></td>
</tr>
<?php endif; ?>
<?php endfor; ?>
But it would be better to use CSS :last-child selector.
You can use this one as a solution:
$last = end($news);
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><?=(($last == $news_item)?"":"<hr />")?></td>
</tr>
<?php endforeach; ?>
</table>
Where the $last is the last array of your array XD.
Get the last key of the array beforehand and compare it the current one while looping:
$keys = array_keys($news);
$last_key = end($keys);
foreach ($news as $news_key => $news_item) {
if ($news_key == $last_key) {
// at the last item
} else {
// not at the last item
}
}
Also you could count the elements in your array beforehand, and have a counter inside the loop that increments on every round, so you can tell if you are at the end or not.

I can't get div id displayed in loop in Code Igniter

I have a command that creates a loop:
foreach($cart as $line=>$item).
When it displays, it shows the HTML quoted below. I have tried to add a div id below the loop, as I want to create a getElementById() function. However, it creates a loop above on its own. Also, I cannot change the tr statement. I have actually done a search and replace on every tr statement in Code Igniter and it is still there. I am quite a novice, and would really appreciate any advice, as this has been baffling me for three days now.
<tbody id="cart_contents">
<?php
if(count($cart)==0)
{
?>
<tir><td colspan='8'>
<div class='warning_message' style='padding:7px;'><?php echo $this->lang->line('sales_no_items_in_cart'); ?></div>
</tr></tr>
<?php
}
else
{
echo "</tr><tir>";
foreach($cart as $line=>$item)
{
?>
<td id = " <?php echo $item['name'];?>" 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>
<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
}
}
?>
</tbody>
</table> </div>
This is the HTML output from the original loop.
<tr><td id=" test" style="align:center;">test</td>
<td><input type="text" name="price" value="150.00" size="6"></td>
<td>
etc...
</td>
</tr>
I don't believe your problem is with your php, I think it is your html.
Your html opening and closing tags do not all match up. You have several unclosed <tr> elements, at least one un-opened <tr>, your form tags don't work out given your if/else statement structure, and you don't actually have an opening <table> tag or an opening <div> tag at the top of your code while you have both at the end. The div that you are getting above your loop that you expect to occur IN your loop is probably the result of your browser trying to interpret the broken page structure.
As far as the form tags I mentioned, make sure that if you use an if statement to open a form, you must also have a condition that will open any required forms if the if statement evaluates to false. I think you have a form element inside an if, then form inputs as part of the next else statement.
I've simplified your posted code and included comments to try and illustrate this:
<tbody id="cart_contents">
<?php if(){?>
<tir><!--what's a 'tir'?, did you mean 'tr'?-->
<td colspan='8'>
<div class='warning_message' style='padding:7px;'>
</div>
</tr>
</tr>
<?php }else{
echo "</tr><tir>";//here you are closing a row that was never opened, and another 'tir'.
foreach($cart as $line=>$item){?>
<td></td>
<?php if (){ //here you call a form input before the form is created...?>
<td><?php echo form_input();?></td>
<?php }else{?>
<td></td>
<?php echo form_hidden();
//this form isn't in a table element, which might act weird?>
<?php }?>
<td><?php if() {
echo form_hidden();
}else{
echo form_input());
//here you have a form input outside of a form if
//the previous if statement evaluates to false.
}?>
</td>
<td></td>
<?php if(){ }
else {
if(){}
else{}
}?>
</td><!--this td was never opened-->
<td> </td>
<td></td>
<td></td>
</tr><!--this tr was never opened-->
<tr><td></td></tr>
</form><!--I don't believe you can count on your conditional statements to garantee that a form was opened-->
<?php }
} ?>
</tbody>
</table> <!--you haven't opened your table in this code-->
</div><!--you havent' opened a div in this code-->

Checking the value of all a loops value

I have this loop in PHP that echoes out a table of results,
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<!--<td><b>Uploaded By</b></td>-->
</tr>
<?php $colours = array("#f9f9f9", "#f3f3f3"); $count = 0;?>
<?php foreach ($allContentImages as $contentImages) : ?>
<tr bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
<td><?php echo "<a href='#' class='screenshot' rel='/media/uploads/$contentImages[categoryId]/$contentImages[contentImageName]'>".$contentImages['contentImageName']; ?></td>
<td><?php echo $contentImages['contentImageType']; ?></td>
<td><?php if($contentImages['isHeadlineImage'] == 1){ echo "Y";}else{echo "N";} ?></td>
<td><?php echo $contentImages['contentTitle'] ?></td>
<td><?php echo date("d-m-Y", $contentImages['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete" href="<?php echo base_url();?>dashboard/deleteContentImage/<?php echo $contentImages['contentImageId'];?>"><img src="/media/images/icons/cancel.png" alt="Delete A Category"/></a></td>
</tr>
<?php
if($contentImages['isHeadlineImage'] == '0') {
echo "<tr bgcolor='red'>";
echo "<td><p>You need to assign a headline image</p></td>";
echo "</tr>";
?>
<?php endforeach; ?>
</table>
I need to check for each content piece with the same title that there is headline image, and if not then echo a new row that is red...but all I get is a new row every time there is an image that is not a headline image. Can anyone help me? I don't mind using javascript if that helps to match the values of the td's? But obviously my attempt is not correct.
It sounds like what you want to do has to be precalculated, since the foreach loop can not know about future content.
Maybe something like this before your foreach statement:
$contentHasHeadlineImage = array();
foreach ($allContentImages as $contentImages) {
if ( $contentImages['isHeadlineImage'] == 1)
$contentHasHeadlineImage[ $contentImages['contentTitle'] ] = true;
}
And then you can use
if (array_key_exists($contentImages['contentTitle'], $contentHasHeadlineImage)) {
// Has headline image...
}
to to verify if a certain title has a headline.
Try my code below:
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<td>Action</td>
</tr>
<?php
$colours = array("#f9f9f9", "#f3f3f3");
$num_colours = count($colours);
$i = 0;
?>
<?php foreach ($allContentImages as $row) : ?>
<tr bgcolor="<?php echo $colours[($i++) % $num_colours]; ?>">
<td><a href="#" class="screenshot"
rel="/media/uploads/<?php echo $row['categoryId']; ?>/<?php echo $row['contentImageName']; ?>">
<?php echo $row['contentImageName']; ?>
</a></td>
<td><?php echo $row['contentImageType']; ?></td>
<td><?php echo $row['isHeadlineImage'] == 1 ? "Y" : "N"; ?></td>
<td><?php echo $row['contentTitle']; ?></td>
<td><?php echo date("d-m-Y", $row['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete"
href="<?php echo base_url(); ?>dashboard/deleteContentImage/<?php echo $row['contentImageId'];?>">
<img src="/media/images/icons/cancel.png" alt="Delete A Category" />
</a></td>
</tr>
<?php if ( $row['isHeadlineImage'] == 0 ): ?>
<tr bgcolor="red">
<td colspan="6"><p>You need to assign a headline image</p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
While tidying your code, I found an unclosed a href in the first column. For the special extra row, I add colspan. I assume $row['isHeadlineImage'] have value only 1 or 0 (integer or boolean).
I also found out that you access array key without using quote. It can become potential bug in the future.
Write the code in tidy indentation and consistent when using block or echoing the php variable will help you found the bug quickly. Also, inspect the result in browser using Firebug in firefox or Web Inspector in Safari and Google Chrome to see if the page structure is like you want to have, all the tag is balanced and closed in right place.

Categories