Hello how do you add php into an unordered list like below ?
<?php // Render the Category List
categoriesList(); ?>
<?php
into
<ul class="oi_smalldev_categories_list">
<li class="cat-item cat-item-7">
Coding
</ul>
Thank you.
This should work:
<ul>
<?php
$mycats = categoriesList();
foreach ($mycats as $cat) {
echo '<li>'. $cat. '</li>';
}
?>
</ul>
I'm guessing you want to take the results from the function and add them to your unordered list.
Basically, in your function you can create a string that is the HTML you want to output. In this case multiple tags.
You can then store it in a variable and call it with PHP further down the page.
Not the best way to handle it but I think this can do what you're after.
So, here's some pseudo code to handle that.
<?php // Render the Category List
$results = categoriesList();
?>
function categoriesList(){
return '<li>'.'category1'.'</li>'.'<li>'.'category2'.'</li>'.'<li>'.'category3'.'</li>';
}
<ul class="oi_smalldev_categories_list">
<?php echo $results; ?>
</ul>
Related
managed to create array tree with childs. Looks like this.
Want to try printing it out with same levels.
<ul>
<li>cat1</li>
<ul><li>sub-cat</li>
<ul><li>subsub-cat</li>
<ul><li>subsubsub-cat</li>
<ul><li>subsubsubsub-cat</li></ul>
<li>subcub-cat2</li>
<ul></ul></ul></ul></ul>
<li>cat2</li>
</ul>
Need to use iterative angle. At least point me to to the right direction.
It doesn't have to be with UL LI, can be with -- -- or some sort similar, just need to print it out using iterative methods.
Try this piece of code:
<ul>
<?php parse($array); ?>
</ul>
<?php
function parse($array) {
foreach ($array as $value) {
?>
<li><?php echo $value->name; ?>
<?php if (!empty($value->child)) { ?>
<ul>
<?php parse($value->child); ?>
</ul>
<?php } ?>
</li>
<?php
}
}
?>
I've already faced this problem and used the same trick to handle.
It is not the exact solution but you can use something like this
foreach( $array as $item ){
echo $item['id'].'<br>';
while(isset($item['child'])){
$item = $item['child'];
echo $item['id'].'<br>';
}
}
here is the code sample that i generated
http://sandbox.onlinephpfunctions.com/code/068f8b2d79cb5e210a6ee4c8e14c1a8649ab0dea
I am trying to set a condition for php- mysql pagination so that it can change the current page "li" to "li class="active" " to mark the current selected page. I am new to php and searching for such pagination tutorial but no luck.
I have done so far what is working but not able to mark selected page. Here $id is for detecting the current page id. How can I set if condition ( or other) so that I can mark the current page item in the pagination? Thousands thanks for helping me.
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
<?php if($id!=$page)
//3!=4
{?>
<li>Next</li>
<?php }?>
</ul>
You could change your for loop from
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
to:
<?php
for($i=1;$i <= $page;$i++){
$class=($i==$id)? ' class="active"' : '';
echo '<li'.$class.'>'.$i.'</li>';
}
?>
If I've understood your code properly, $page represents total pages and $id represents the current page, this will set the current page number as the active class and leave the other pages without the class
Assuming that $id is the current page number, and that $page is the total number of pages, you need to highlight only one by doing the following in your loop:
if($i==$id) // highlight
else // don’t highlight
Your main errors are that you didn’t test whether $i==$id, and you didn’t have an alternative unhighlighted version.
I really think that you should simplify your code by separating your logic from the HTML. It becomes very unreadable otherwise, and very hard to manage.
I have taken the liberty of doing just that. This way you can see where the logic does the hard work, an you only need to print the results in the HTML.
<?php
$id=3; // test value
$page=20; // test value
$li=array(); // temporary array for convenience
$template='<li%s>%s</li>';
if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
for($i=1;$i<=$page;$i++) {
if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i); // Current
else $li[]=sprintf($template,'',$i,$i);
}
if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
$li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>
You will also see two techniques to make things easier:
I use an array as a temporary container. It is easier to add items this way and then to implode it into a string afterwards
sprintf makes it easier to define a template string, so you can manage the HTML component more easily, and fill in the gaps when the time comes.
This is my first question.
I'm building a simple dynamic menu using <li>
I'm working on a PHP based CMS (Kirby)
Kirby has predefined PHP functions (helpers)
I'm trying to output a <li> for each page on the website:
<li class='active'><a href='page1'></a></li>
<li><a href='page2'></a></li>
<li><a href='page3'></a></li>
...
Im using a PHP function e($condition, $value) to style the menu item only if that page isOpen()
// I need help here
<?php
foreach ($pages->visible() as $p):
echo "<li" . e($p->isOpen(), ' class="active"') . "><a href='" . $p->url() . "'></a></li>";
endforeach;
?>
The function is working but the css part class="active" is printing OUTSIDE the <li> on the final code
class="active"
<li>...</li>
<li>...</li>
<li>...</li>
I had this previous code that worked fine, but since i'm using display: inline-block the menu had spaces betwes each block, since the following code was placing each <li>in a new line.
// This code works
<?php foreach($pages->visible() as $p): ?>
<li <?php e($p->isOpen(), ' class="active"') ?> ></li>
<?php endforeach ?>
The reason i'm rewrinting the code is to remove the white space between the inline: blockelements.
I'm failing to concatenate the string in a way that the function works and print its results inside the <li>tag.
I've searched here and also have read lots of Docs in php.net but nothing worked to me, I'm struggling with this for 2 days.
I'm expecting to learn better how and when to use concatenation and string operators.
The problem is that Kirby's e() function already has the echo in the routine, instead of just simply returning the value.
http://getkirby.com/docs/cheatsheet/helpers/e
If you were to change your output loop to something more like this, echoing separately, you'll get the results in the desired order:
foreach($pages->visible() as $p)
{
echo "<li";
e($p->isOpen(), ' class="active"');
echo "> and the rest of your line </li>";
}
That said, perhaps using e() isn't the most elegant in this case. Maybe try the r() function instead:
http://getkirby.com/docs/cheatsheet/helpers/r
foreach($pages->visible() as $p)
{
echo "<li ".r($p->isOpen(), ' class="active"').">more text</li>";
}
Sorry if this seems very simple I'm rather new to php.
I'm drawing data from the database using a for each loop and want to add a list item for each result to a side bar while adding a new container div to the body.
What is the best way to code this?
Any help is much appreciated.
Thanks A
Edit...
My code so far...
<?php
$iidcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_iid_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
$gadcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_gad_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
?>
<ul>
<?php
foreach ( $iidcoversheets as $iidcoversheet )
{
?>
<li><?php echo $iidcoversheet->business name; ?></li>
<?php
}
?>
<?php
foreach ( $gadcoversheets as $gadcoversheet )
{
?>
<li><?php echo $gadcoversheet->business name; ?></li>
<?php
}
?>
</ul>
I understand how to add the <li> items but I'm strugging on how to add a div containing some html to another div not in the <ul> tag as it seems to me this will interrupt the code for the <ul>.
Any help would be really appreciated.
Thanks A
<div>
<ol>
<?php
foreach ($uls as $ul) {
?>
<li>
<?php echo $ul; ?>
</li>
<?php
}
?>
</ol>
</div>
Ok I came to the realisation that I could simply do another exact same loop later on in the page in the div I wanted but with different output. I was trying to do it all up front save the results and output them somewhere else, unnecessary.
Clearly I was having a daft moment earlier.
Thanks for all the input.
Regards
A
I'm working on my own simple Content Management System to implement on my projects for clients who want to do their own maintenance.
I'm going to contain only page titles and their content in a database table. All I want is to make a list which takes all the page titles and puts them into a <ul>. I believe I should use foreach, but I'm not too good at PHP, so I'd appreciate some help.
So how would I go about this?
<?php
$qry = mysql_query("SELECT * FROM links");
?>
<ul>
<?php while($row = mysql_fetch_array($qry)) { ?>
<li><?php echo $row['link_title']; ?></li>
<?php } ?>
</ul>
...in case your table with links has the colums 'link_url' and 'link_title'. But you get the idea, I guess.
If you have retrieved an array with the table's rows, you can probably do something like this:
The PHP-in-HTML version:
<ul>
<?php foreach( $pages as $page ): ?>
<li><?=$page['title']?></li>
<?php endforeach ?>
</ul>
The HTML-in-PHP version:
echo "<ul>";
foreach( $pages as $page ) {
echo "<li>$page['title']</li>";
}
echo "</ul>";
In both versions $pages is an array containing all the rows from the table.
Maybe you can adapt that to your needs?