for ($i = 0; $i < count($item); $i++) {
echo '<ul class="list1">';
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
echo '<ul class="list2">';
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
}
I tried to display the following HTML with only php(above code) however, it seems like it's not working as expected. How can I display the exact same thing with php?
<ul class="list1">
<li>
<? echo $item[0];?>
</li>
<li>
<? echo $item[1];?>
</li>
</ul>
<ul class="list2">
<li>
<? echo $item[2];?>
</li>
<li>
<? echo $item[3];?>
</li>
<li>
<? echo $item[4];?>
</li>
<li>
<? echo $item[5];?>
</li>
<li>
<? echo $item[6];?>
</li>
<li>
<? echo $item[7];?>
</li>
</ul>
do something like this,
<?php
$item = ['1','2','3','4','5','6','7','8'];
for ($i = 0; $i < count($item); $i++) {
if ($i < 1) {
echo '<ul class="list1">';
}
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 1) {
echo '</ul>';
}
if ($i == 2) {
echo '<ul class="list2">';
}
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 7) {
echo '</ul>';
}
}
?>
this is the simplified syntax for the expected output
echo '<ul class="list1">';
for ($i = 0; $i < count($item); $i++) {
echo '<li>'.$item[$i].'</li>';
if ($i == 1) {
echo '</ul><ul class="list2">'';
}
}
echo '</ul>';
Try use two for loop, first loop items from 0 to 1 in "list1", second loop items form 2 to 7 in "list 2"
echo '<ul class="list1">'
for ($i = 0; $i <= 1; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';
echo '<ul class="list2">'
for ($i = 2; $i <= 7; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';
Related
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>
I want to detect virtuemart category page in mod_breadcrumbs.
Breadcrumbs is loading all page and i just wanted write a message in category page.
My breadcrumbs code:
<div class="breadcrumbs<?php echo $moduleclass_sfx; ?>">
<?php
echo '<ul>';
for ($i = 0; $i < $count; $i ++) {
// If not the last item in the breadcrumbs add the separator
if ($i < $count -1) {
if (!empty($list[$i]->link)) echo '<li>'.$list[$i]->name.'</li>';
else echo '<li class="pathway">' . $list[$i]->name . '</li>';
if($i < $count -2) echo ' <li class="pathway separator">></li> ';
} else if ($params->get('showLast', 1)) { // when $i == $count -1 and 'showLast' is true
if($i > 0) echo ' <li class="pathway separator">></li> ';
echo '<li class="pathway">' . $list[$i]->name . '</li>';
}
}
echo '</ul>';
?>
</div>
Here's how you can check if you're on a category view:
$appInput = Jfactory::getApplication()->input;
if($appInput->getCmd('option')=='com_content' && $appInput->getCmd('view')=='category' ){
//add your code here
}
I have a piece of code which executes a calendar. On this calendar certain names have to appear. However, on each date appears only one name, while in the database some dates hold up to 20 names.
This image should hopefully clarify things: http://imgur.com/a31pj0s
In this image you can see each date, which on some of them one name. As stated before, most of these contain many more names.
Now, I understand why this does not work, as it executes only once, but how can I get it to actually work? Please help me out :)
Here is also a link to the same piece of code, except easier to read: http://codepad.org/tVSzHDTx
<?php
$j = 2;
$i = 1;
//query the database
$query = mysql_query("SELECT * FROM months");
//fetch the results / convert results into an array
echo '<div class="accordion" id="calendar">';
while($rows = mysql_fetch_array($query)) {
$month = $rows['month'];
$id = $rows['id'];
echo '
<div class="accordion-group">
<div class="accordion-heading" id="month_'.$id.'">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#calendar" href="#monthsid'.$id.'">
'.$month.'
</a>
</div>
<div id="monthsid'.$id.'" class="accordion-body collapse">
<div class="accordion-inner">';
$n = $rows['num_of_days'];
echo '<ul class="list-group" id="'.$id.'" style="margin-bottom: 0;">';
for ($i = 1; $i <= $n; $i++) {
if ($j == 7) {
echo '<li class="list-group-item" id="'.$i.'" style="margin-bottom: 5px;">';
} else {
echo '<li class="list-group-item" id="'.$i.'">';
}
echo '<a data-toggle="modal" href="#myModal" class="add_member" id="'.$month.' '.$i.'">+</a>
<span class="badge">'.$i.'</span>';
if ($i == date("j")) {
echo '<div class="day_segment current_day">';
} else {
echo '<div class="day_segment">';
}
if ($j == 1) {
echo '<i style="color: #aaa;">'.$i.' |</i> Monday';
$j++;
} else if ($j == 2) {
echo '<i style="color: #aaa;">'.$i.' |</i> Tuesday';
$j++;
} else if ($j == 3) {
echo '<i style="color: #aaa;">'.$i.' |</i> Wednesday';
$j++;
} else if ($j == 4) {
echo '<i style="color: #aaa;">'.$i.' |</i> Thursday';
$j++;
} else if ($j == 5) {
echo '<i style="color: #aaa;">'.$i.' |</i> Friday';
$j++;
} else if ($j == 6) {
echo '<i style="color: #aaa;">'.$i.' |</i> Saturday';
$j++;
} else if ($j == 7) {
echo '<i style="color: #aaa;">'.$i.' |</i> Sunday';
$j = 1;
}
echo '</div>';
$posts_query = mysql_query("SELECT * FROM posts WHERE day=$i ");
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
echo '</li>';
}
echo '
</ul>
</div>
</div>
</div>
';
} /* End While Loop */
echo '</div></div>';
?>
As mentioned in the comments, you should not use the mysql_ functions as they are deprecated and not secure. Use MySQLi or PDO instead.
To answer your question, the code block below is the reason why it only displays one entry per day. In the while loop, you overwrite all the four variables each time, thus only the last one will be displayed in your for loop. The for loop is not necessary.
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
Try changing your code to something like this:
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
I have this php foreach loop
<?php $all_news = $this->db->query("Select * from news"); ?>
<div class="inner">
foreach($all_news as $key=>$news){?>
<div class="news <?php echo ($key%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
But the problem is the $all_news may have 20 results or so but the design only allows me to put for 4 news blocks in each inner div...is there a way make this happen so i have only 4 news divs in each inner div
<?php
$all_news = $this->db->query("Select * from news");
echo '<div class="inner">';
$c = count($all_news);
for($i = 0; $i < $c; $i++){
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
if($i % 4 == 3)
echo '</div><div class="inner">';
}
echo '</div>';
?>
Change your query to only return 4 rows:
SELECT * FROM news LIMIT 4
Alternatively you can change your for-loop.
for($i = 0; $i < min(4, count($all_news)); $i++)
{?>
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$i]['title'];
<?}
[edit]
See what you mean now. Create two loops:
<?
$index = 0;
while ($index < count($all_news))
{
$news = $all_news[$index];
?>Start outer div<?
for ($item = 0; $item < 5; $item++)
{
?>Inner div with news item <? echo $news['title'];
}
?>End outer div<?
$index++;
}
You could use two for loops:
<?php $all_news = $this->db->query("Select * from news"); ?>
<?php for($i = 0, $l = count($all_news); $i < $l; $i+=4): ?>
<div class="inner">
<?php for($j = $i; $j < $i+4; $j++): ?>
<div class="news <?php echo ($j%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$j]['title'] ?>
<?php endfor;?>
</div>
<?php endfor;?>
Another option would be array_chunk [docs].
The laziest way would be to just check whether or not you've already done four in the current div on the fly. If you have, close the current div and start a new one:
<div class="inner">
<?php
foreach ($all_news as $key => $news) {
if ($key % 2) {
$oddEven = 'odd';
} else {
$oddEven = 'even';
if ($key && $key % 4 === 0) {
echo '</div><div class="inner">';
}
}
echo "<div class=\"news $oddEven\">";
// ...
}
?>
</div>
Note that this assumes $all_news has an element at 0, so it makes sure that it doesn't close the first, empty div.
I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?
<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically
I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}