Loop with a Loop including an explode() - php

I have an associative array returning a SQL query that pulls questions from my database. The tags field is stored as a delimited text VarChar. I want to loop through all of my database entries while using explode to create an array and then loop through the tag array to create a UL with the tags as well unfortunately it has not worked very well. This is what I have so far:
<?php foreach($questionRow as $questionShow) { ?>
<?php echo ($questionShow['netvotes']) ;?>
<?php echo ($questionShow['views']) ;?>
<?php echo ($questionShow['q_answer_count']) ;?>
<?php echo ($questionShow['title']) ;?>
Tags:
<ul style="display: inline">
<?php
$tagname = explode(",",$questionShow['tags']);
foreach ($tagname as $tagList) { ?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo($tagList) ;?></li>
<?php }; ?>
</ul>
<?php }; ?>
The results were very strange
I also tried by using the below to run my outer loop
<?php while($questionShow =$questionResult>fetch_assoc() ) { ?>
That caused the main recordset results to not actually loop. Hopefully I have explained this properly and someone could help. Thank you in advance!

well this could be very simple:
<?php
$query = mysql_query("SELECT * FROM `questions` LIMIT 30"); // replace this with your query
while($data=mysql_fetch_array($query)){
echo $data['netvotes'];
echo $data['views'];
echo $data['q_answer_count'];
echo $data['title']) ;?>
?>
Tags:
<ul style="display: inline">
<?php
foreach(explode(",",$data['tags']) as $tag){
?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo $tag; ?></li>
<?php
}
?>
</ul>
<?php
}
?>

Related

Issue in pulling pages from the database

I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>

For each inside other for each on view MVC PHP

I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>

While inside another while

I am trying to display data from two different Mysql tables in the same html table. Do do that I have put a while loop inside another while loop. The problem is that in the table the data of the second loop is displayed only on the first raw of the html table. I really don't know what is wrong. Thanks for your help.
<?php
while($results = mysql_fetch_array($raw_results)){
echo '<td>';
echo '<a href="linkto.php?data='.ucfirst($results['song_name']).'" class="iframe"><img src="icons/1384070574_95.png"> ';
echo '<a href="'.ucfirst($results['song_name']).'" class="clapping">';
echo '<img src="icons/Hand-icon.png" id="songs'.$pictureid.'" onClick="action(\'songs'.$pictureid.'\')"/></a>';
echo '</td>';
echo '</tr>';
if (isset($userid)){
echo '<td>
<div id="cssmenu">
<ul>
li class="has-sub><span><img src="icons/1384074758_document_music_add.png"></span>
<ul>
<li class="has-sub"><span>Create a Playlist</span></li>';
while($playlist = mysql_fetch_array($raw_playlist)){
echo'<li class="has-sub"><span>'.ucfirst($playlist['title']).'</span></li>';
}
echo'</ul>
</li>
</ul>
</div>
</td>';
}
echo' </tr>
';
$pictureid = $pictureid + 1;
$songid= $songid + 1;
}
echo '</tbody>';
?>
As mentioned in the comments, you may have a problem with your nested data depending on what you're trying to show.
However, your HTML is a mess, missing some opening and closing tags, which may mean that some data is being sent to the browser but not being displayed. Have you looked at the source of the page in the browser to check the data is not there? Until you do, we cannot be sure whether the missing data is an HTML, SQL or PHP error.
You should try to limit the amount of HTML that you echo from PHP for this reason, and I've tidied it up a bit and added missing tags below. See if that helps at all.
<?php while ($results = mysql_fetch_array($raw_results)) : ?>
<tr>
<td>
<img src="icons/1384070574_95.png">
<img src="icons/Hand-icon.png" id="songs<?php echo $pictureid; ?>" onClick="action('songs<?php echo $pictureid; ?>')" />
</td>
</tr>
<?php if (isset($userid)) : ?>
<tr>
<td>
<div id="cssmenu">
<ul>
<li class="has-sub><span><img src="icons/1384074758_document_music_add.png"></span>
<ul>
<li class="has-sub"><span>Create a Playlist</span></li>
<?php while ($playlist = mysql_fetch_array($raw_playlist)) : ?>
<li class="has-sub"><span><?php echo ucfirst($playlist['title']); ?></span></li>
<?php endwhile; ?>
</ul>
</li>
</ul>
</div>
</td>
<?php endif; ?>
</tr>
<?php
$pictureid++;
$songid++;
endwhile;

jQuery Sortable Lists with CodeIgniter

I am attempting to make a sortable list out of list items populated from the database using the jQuery plug in but the effect is only applied to the first item presented:
<?php if(isset($bookmarks)) : foreach($bookmarks as $row) :?>
<div id="makeDrag">
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small">
<?php echo anchor("site/delete/$row->id", "Delete"); ?>
</h4>
</li>
</div>
<?php endforeach; ?>
I can kind of see where this is going wrong but do not know how to fix it. I would obviously like the effect to affect all the populated li not just the first one. Any help would be great. Sorry if I am unclear, I can try and rephrase things if this is confusing.
The cause is likely because you have
$('#makeDrag').sortable();
but you also have a foreach statement that creates multiple #makeDrag elements thus making your HTML invalid.
To fix this:
<?php if(isset($bookmarks)) : ?>
<ul id="makeDrag">
<?php foreach($bookmarks as $row) : ?>
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small"><?php echo anchor("site/delete/$row->id", "Delete"); ?></h4>
</div>
</li>
<? endforeach; ?>
</ul>
<?php endif; ?>
HTH

Adding a class to the last row in a foreach loop

HI all,
This code works perfectly printing all 5 results with borders on the bottom of each list item through CSS.
However the last item id like there to be no border. How could i add a class to the list item on the last iteration?
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php foreach ($last_articles as $article): ?>
<li>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php endforeach; ?>
</ul>
Thanks for your help.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
BTW, this adds the class "last-item" to the last item processed.
And yes, you should restructure your code so its readable.
You can use the :last-child CSS modifier:
ul.latest-news:last-child { border-bottom: none; }
But it's not so widely supported (yet), so adding an explicit .last class as you're about to do is probably best.

Categories