While inside another while - php

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;

Related

Echo html code from php to html

I'm working on my own project for a music site and i'm blocked on a situation. Right now i'm working on the playlist and i want to echo from php(i'm working with odbc connection) on html, but it's not putting all the data, only the first row, and i want all the data from database to be echo in the playlist.
I tried to make an echo statement in php which to send row by row with html code.
This is my PHP:
<?php
error_reporting(6);
include("connect.php");
include("checklogin.php");
$username = $_SESSION['username'];
//echo $username;
$sql="select name from [YourTube].[dbo].[yt_url]
where username='$user_check'";
$rs=odbc_exec($conn,$sql);
//echo $sql;
while(odbc_fetch_row($rs)){
$name = odbc_result($rs, 1);
//print("$name\n");
}
?>
And i have tried to put something like this in php>>
<?php if($rs) { ?>
<li class="list_item selected">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</li>
<?php } ?>
And the result is just the first row from database, but if i print $name it's showing all the rows.
The html code for playlist:
<div class="list_wrapper">
<ul class="list">
<li class="list_item selected">
<div class="info">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</div>
</li>
</ul>
</div>
And like i said, this code on html it's showing only one row in playlist, not all of them. And i'm thinking to do something like echo from php in html code for every row something like this:
<li class="list_item selected">
<div class="info">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</div>
</li>
Can someone help me with this? I'm working on this thing for some days and i can't figure out how to do it.
Thank you in advance!
Bogdan
You have to apply while() inside if() like below:-
<?php if($rs) {
while(odbc_fetch_row($rs)){
?>
<li class="list_item selected">
<div class="title" action="../php/songs.php"><?php echo odbc_result($rs, 1)?></div>
</li>
<?php }
} ?>

JQuery Plugin with database records- Codeigniter

I have found following JQuery plugin to show the records in alphabetical orders.
http://esteinborn.github.io/jquery-listnav/
I am using demo one. I have implemented in codeigniter and got successful to print all the records. However, all the records appearing only under "All" option. It does not display records alphabetically.
I am not sure either am I using this plugin in the correct way ? Following is my code:
<?php if (!$companies): ?>
<tr>
<td colspan="4">No companies in the system yet...</td>
</tr>
<?php
else :
?> <ul id="demoOne" class="demo">
<?php
$i = 0;
foreach($companies as $company) :
?>
<li>
<?php print $company->title; ?>
</li>
</ul>
</tr>
<?php
$i++;
endforeach;
endif;
?>
Following is script:
<script>
$(function(){
$('#demoOne').listnav();
});
</script>
Your html doesn't seem to be correct. Try:
<?php if (!$companies): ?>
<tr>
<td colspan="4">No companies in the system yet...</td>
</tr>
<?php else : ?>
<tr>
<ul id="demoOne" class="demo">
<?php
$i = 0;
foreach($companies as $company) :
?>
<li>
<?php print $company->title; ?>
</li>
<?php
$i++;
endforeach;
?>
</ul>
</tr>
<?php
endif;
?>

Echos not working

I am trying to make a site using cakephp and twitter bootsrap. For the gallery I am making I am trying to display, using foreach, multiple images , but they simply don't show up. Everything is working, the foreach and the variable data, but the echos don't display anything. Could anyone help please?
Thanks in advance.
(code bellow from my show_images.ctp view)
<table >
<tr>
<?php
$i=0;
foreach( $gallery_images as $gallery_image ):?>
<td align=center>
<?php echo "<a id=\"single_1\" href=\"test/".$gallery_image['GalleryImage']['path']." ";?>
<?php echo "<img src=\"test/".$gallery_image['GalleryImage']['path'].", alt=\"".$gallery_image['GalleryImage']['path']."";?>
<?php echo "</a>";?>
</td>
<?php $i++;?>
<?php
if($i==4){
echo "</tr><tr>";
$i=0;
}
?>
<?php endforeach ?>
</tr>
you haven't close your tag inside echotry to change this:
<?php echo "<a id=\"single_1\" href=\"test/".$gallery_image['GalleryImage']['path']." ";?>
<?php echo "<img src=\"test/".$gallery_image['GalleryImage']['path'].", alt=\"".$gallery_image['GalleryImage']['path']."";?>
<?php echo "</a>";?>
to this:
<?php echo '<a id="single_1" href="test'.$gallery_image['GalleryImage']['path'].' >';?>
<?php echo '<img src="test'.$gallery_image['GalleryImage']['path'].'" alt="'.$gallery_image['GalleryImage']['path'].'" />';?>
<?php echo '</a>';?>

Loop with a Loop including an explode()

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
}
?>

hidden div not working correctly in for loop using a modal box

I have a mysql select and it spits out all the results in a <ul> that I have setup in a for loop. This works great. However, I wanted to have a "click more" type link at the bottom of each result that opens in a modal box when clicked. I also got this working just fine. What doesn't work, however, is the data inside what is displayed inside the modal box. It displays the data correctly but it is from the first result no matter what result you click. I thought I just didn't have my for loop setup correctly but it works when I change the id of the div the content is in for the modal box (but then the modal box doesn't work obviously).
Is there some reason why hiding a div would mess a for loop up as far as which row to display?
Below is the for loop
for($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
?>
<div id='basic-modal'>
<ul style="background-color:#F5F5F5; padding:2px;">
<li class="program" style="font-size:18px;"><strong><?php echo trim ($row["program"]); ?></strong></li>
<li><strong>Sponsored by:</strong> <i><?php echo trim ($row["organization"]); ?></i></li>
<li><strong>Discipline:</strong> <?php echo trim ($row["discipline"]); ?></li>
<li><strong>Mission:</strong> <?php echo trim ($row["mission"]); ?></li>
<li><strong>Description:</strong> <?php echo trim ($row["content"]); ?></li>
<li><strong>Grade(s):</strong> <?php echo trim ($row["grade"]); ?></li>
<li><strong>Cost:</strong> <?php echo trim ($row["cost"]); ?></li>
<li> </li>
<li>Contact Info</li>
</ul>
<br />
<br />
</div>
<!-- pop up contact info -->
<div id="basic-modal-content">
<ul style="font-size:12px">
<li class="program" style="font-size:18px;"><strong><?php echo trim ($row["program"]); ?></strong></li>
<li><strong>Sponsored by:</strong> <i><?php echo trim ($row["organization"]); ?></i></li>
<li><strong>Contact Person:</strong> <?php echo trim ($row["contact"]); ?></li>
<li><strong>Contact Email:</strong> <?php echo trim ($row["email"]); ?></li>
<li><strong>Contact Phone:</strong> <?php echo trim ($row["phone"]); ?></li>
<li><strong>Contact Hours:</strong> <?php echo trim ($row["contactHours"]); ?></li>
</ul>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/x.png' alt='' />
</div>
<?php
}
?>
I'm using Eric Martin's SimpleModal if that makes a difference.
Thanks for the help.
You have same id of each div it is wrong

Categories