I'm using CodeIgniter and I want to display a list of cities in multiple columns in html like this where i can just adjust the number of items inside the <ul> tag and then generate another <ul> tag for the next column
----------------
City name 1 City name 5 City name 9
City name 2 City name 6 City name 10
City name 3 City name 7
City name 4 City name 8
-------------------
I dont know how to make it display like that but here's my current code that just display everything in 1 column.
<ul>
<?php
foreach($row_city as $city):
echo "<li>".anchor("#",$city->city_name)."</li>";
endforeach;
?>
</ul>
You don't need to use tables. Simply use this code:
$i = 0;
echo "<ul>";
foreach($row_city as $city):
if ($i != 0 && $i%4 == 0) {
echo "</ul><ul>";
}
echo "<li>".anchor("#",$city->city_name)."</li>";
$i++;
endforeach;
echo "</ul>";
and just float the lists next to each other.
You can try to display it inside a table like that with that html code:
<table>
<tr>
<td>
<ul>
<li>element1</li>
<li>element1</li>
<li>element1</li>
</ul>
</td>
<td>
<ul>
<li>element2</li>
<li>element2</li>
<li>element2</li>
</ul>
</td>
<td>
<ul>
<li>element2</li>
<li>element2</li>
<li>element2</li>
</ul>
</td>
</table>
And then you must update your code to make it work with more column.
Here an example of php code that show that kind of output:
<table>
<?php
echo '<td><ul>';
$j =4;
for($i = 0; $i<10; $i++ ){
if($j==0){
echo '</ul></td><td><ul>';
$j=4;
}
echo "<li>".$i."</li>";
$j--;
}
echo '</ul></td>';
?>
You can adapt it to your problem (try to replace the for with your foreach).
Related
I want to fetch some data from database in the form of list. Following is my loop:
<div class="col-sm-4">
<ul>
<?php
foreach($subjects as $s)
{
echo "<li>$s->subject_title</li>";
}
?>
</u>
</div>
Now this loop prints data in one column and the user have to scroll down to view the last elements. I want to control the loop in such a way that after printing first 10 elements, it must start again from the div tag and print the next 10 elements and so on. I want to show the data in such a way that the user don't have to scroll down.
For example, I want to print the data in the following way:
I write the following code but it is not working. The div tag is only applied to the first 10 elements, and not on the rest of the data.
<div class="col-sm-6">
<ul class="filter-list">
<?php
$i = 0;
foreach($main_subjs as $i=>$ms)
{
if($i == 10) {
echo "</ul></div><div class='col-sm-6'><ul>";
$i++;
continue;
}
echo "<li><a href='#'>$ms->mains_title</a></li>";
}
?>
</ul>
</div>
Please Help.
As an arugment for a loop you can also use the key of array
foreach($subjects as $i=>$s)
When you will be able to "control" your loop, e.g.:
<div>
<?php
foreach($subjects as $i=>$s) {
if($i> 0 && $i % 5 == 0) {
echo "</div><div>";
}
echo "<li>$s->subject_title</li>";
}
?>
</div>
I currently have a simple html list. What I want is to repeat this list item with each repeated element having a variable that will increase by one, each time it is repeated.
The php calls that i have included in the above list item is from the Advanced Custom Field plugin on WordPress. The variable I want to change is the number inside of those php calls.
<ul>
<li>
<?php the_field('vendor_1_name'); ?> <!--the number is the variable that i want to increase by one with each repition-->
<a href="http://<?php the_field('vendor_1_url'); ?>" target="_blank"/>
<?php the_field('vendor_1_url'); ?>
</a>
</li>
</ul>
I have an idea of how to do this using an array, but I would love to hear other methods on how to do something like this. Thanks!
<ul>
<?php for ($z = 0; $z < $n; $z++) : ?>
<li>
<?php the_field('vendor_'.$z.'_name'); ?>
<a href="http://<?php the_field('vendor_'.$z.'_url'); ?>" target="_blank">
<?php the_field('vendor_'.$z.'_url'); ?>
</a>
</li>
<?php endfor; ?>
</ul>
Where $n is the number of times it should iterate.
Use a for loop. If you have an array of items (are you grabbing vendors from a database?), just iterate through the array:
<?php
// fetch values and define $vendors
// using sql is highly recommended
echo "<ul>\n";
for ($i = 0; $i < count($vendors); $i++) {
echo "<li><a href='".$vendors[$i]['vendor_url']."'>".$vendors[$i]['vendor_name']."</a></li>\n";
}
echo "</ul>\n";
?>
Note that the array would say vendor_name instead of vendor_1_name because the field is not going to have the iteration in its name.
i need populate a bunch of posts in a page where there are 3 columns:
column 1 |column 2|column 3 |
the result i want to get is:
column 1 |column 2|column 3 |
post1 post2 post3
post4 post5 post6
...
for example,
my html code looks like the following:
post1
post4
...
post2
post5
...
post3
post6
...
how can i do with PHP foreach?
i tried the alternative way using jquery, first populate all the posts in the , then add to wrap every 3 posts, but that's not what i want..
You didn't give a much detail to work with so my example makes a bunch of assumptions. However, you should be able to adapt the php variables and desired html layout based on it:
<table>
<tr>
<?php $i = 0; ?>
<?php foreach($posts as $post): ?>
<?php $i++; ?>
<td><?php echo $post['title']; ?></td>
<?php if($i == 3): ?>
</tr><tr>
<?php $i = 0; ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
</table>
I made a database that has a points system, and to convert this to ranks I planned on showing the data from the database from highest to lowest, then using a list to display it with numbers. However, when displaying it as a list it just shows up as:
1.
1.
1.
1.
1.
1.
(going on until it reaches 10)
Which is not how it should act, I have tried changing the stuff up around the code and the results have only worsened. Can anyone help?
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td><ol><li style='list-style-type:decimal;'> ".$row['points']."</li></ol></td> <td> </td>
<td> </td>
<td> </td></tr>";
}
?>
As mentioned in the comments remove your
<td>
tags or do a table instead of an ordered list and better use ' instead of " for surrounding html tags with attributes in it and use the " for the attribute values, because then you get valid html and no problems in php.
Correct code for list would be:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
echo "<ol>";
while($row = mysqli_fetch_array($mw2ranks))
{
echo '<li style="list-style-type:decimal;"> '.$row['points'].'</li>';
}
?>
</ol>
For a table you could use the following:
<?php
$con=mysqli_connect("localhost","root","","gg");
$mw2ranks = mysqli_query($con,"SELECT * FROM qsmw2 ORDER BY points DESC") ;
?>
<table>
<tr>
<?php
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$row['points']."</td>
}
?>
</tr>
</table
Your HTML is broken. You can't include the <td> elements inside your <ol>. You need to create a structure like this:
<td>
<ol>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
<li> Some stuff </li>
</ol>
</td>
Grouping your <li> elements like this will allow the numbering to work, but obviously it will mess up your table layout. Your alternative is to generate the numbering in your PHP script and not use the ordered list structure at all.
you could use this
<table>
<tr>
<?php
$i=1;
while($row = mysqli_fetch_array($mw2ranks))
{
echo "<td> ".$i."</td>";
echo "<td> ".$row['points']."</td>";
$i++;
}
?>
</tr>
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)".