Nidified mysqli_fetch_assoc while - php

I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?

you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>

Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>

seems that use mysqli_data_seek($result, 0); do the trick the right way.

Related

The problem of cannot pass ?= value in php pagination

I have do a pagination with php&html,the problem is my $page is always equal 1 even I click another page and the url is already become ?page=2.The value $p cannot pass as $page.How can I solve this?
<ul class="nospace clear" style="width:1310px;">
<?php
if(isset($_GET['page'])&& $_GET['page']!=""){$page = $_GET["page"];}else{$page=1;}
$current=$page;
$end=12;
if($page=1){$start=0;$previous=$page;$next=$page+1;}
else if($page<=12){$start=$page*12-12;$previous=$page-1;$next=$page+1;}
else{$start=0;$previous=$page-1;$next=12;}
$sql = "Select * from item where Gender='women' AND Category='cloth' LIMIT $start,$end";
$result = mysqli_query($connect,$sql);
while($row=mysqli_fetch_assoc($result)){?>
<img src="../images/demo/<?php echo $row["Pic"]; ?>" style="width:300px;height:280px"><br><p></p><h3><strong><?php echo $row["Name"]; ?></strong></h3><p><?php echo $row["Description"]; ?></p></li>
<?php } ?></ul><figcaption>Page <?php echo"$page ";?> end...</figcaption>
</figure>
</div>
<nav class="pagination">
<ul>
<li>« Previous</li>
<?php
for ($p=1;$p<13;$p++){
if ($page == $p) {?>
<li class="current btn1"><strong><?php echo $p ?></strong></li><?php }
else{?><li><a href="?page=<?php echo $p ?>" class='btn1'><?php echo $p ?></a></li><?php }}?>
<li> Next » </li>
</ul>

Checking in_array value

The Image above is what I want to achieve, the only problem is am echoing subjects and topics where the topics go under the subjects, but I want to only echo say English and all topics under English will go under English. But what I have is every topic comes with its own Subjects. Thus from the Image Two English Language dropdowns.
Here is the code.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
</div>
</li>
<?php
} else {
echo ""
}
}
?>
This should output the subjects and topics and allows for multiple subjects per topic. Also, the echo "" in the else is un-needed.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$lastSubject = '';
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']) {
if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
}
?>
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
<?php
if($lastSubject != $row['subject']) {
?>
</div>
<?php
$lastSubject = $row['subject'];
}
?>
</li>
<?php
}
}
?>

Resource id #9 showing

So i'm trying to get a list of activities(subjects) of a school and i get them all like it's on the Database but i also gives me Resource id #9 i've tried to remake the code so many times but it gives me every single time this.
1st try
<li>
<?php
if(Session[tipo]<=1){
echo "
<li class='treeview'>
<a href='#'>
<i class='fa fa-share'></i> <span>Atividades</span>
<i class='fa fa-angle-left pull-right'></i>
</a>
<ul class='treeview-menu'>
<li>".
$result = mysql_query("select * from atividade_aluno where ativo=1");
while($row = mysql_fetch_object($result)) {
echo "<a href='lista.php?x=$row->id_atividade'>". utf8_encode($row->atividade)."</a>";
?>
<?php
}
?><?php
echo "
</li>
</ul>
</li>";
}else{
echo "<a href='#'>1</a>";
}
?>
</li>
2nd try
<li>
<?php
if(Session[tipo]<=1){
echo "
<li class='treeview'>
<a href='#'>
<i class='fa fa-share'></i> <span>Atividades</span>
<i class='fa fa-angle-left pull-right'></i>
</a>
<ul class='treeview-menu'>
<li>".
$result = mysql_query("select * from atividade_aluno where ativo=1");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<a href='lista.php?x=".$row["id_atividade"]."'>". utf8_encode($row["atividade"])."</a>";
?>
<?php
}
?><?php
echo "
</li>
</ul>
</li>";
}else{
echo "<a href='#'>1</a>";
}
?>
</li>
PS: The result is the same so i won't upload two pictures of the same.
Thanks you so mutch for your time
The . operator concatenates strings in PHP. For example, this prints "helloworld":
$a = "hello";
$b = "world";
echo $a . $b;
Now, in your code you have this:
<li>".
This concatenates to the next variable ($result). You should change it to:
<li>";
Just change your code to this
<li>
<?php
if(Session[tipo]<=1){
$result = mysql_query("select * from atividade_aluno where ativo=1");
echo "
<li class='treeview'>
<a href='#'>
<i class='fa fa-share'></i> <span>Atividades</span>
<i class='fa fa-angle-left pull-right'></i>
</a>
<ul class='treeview-menu'>
<li>".
while($row = mysql_fetch_object($result)) {
echo "<a href='lista.php?x=$row->id_atividade'>". utf8_encode($row->atividade)."</a>";
?>
<?php
}
?><?php
echo "
</li>
</ul>
</li>";
}else{
echo "<a href='#'>1</a>";
}
?>
</li>
You concatenated $result which is resource id with your string. That is the reason.

Validate echo PHP

Im trying to validate my output data from a php site that Im calling with
<?php include_once('nav.inc.php');
?>
The thing is when Im using this code
<ul>
<li>GÄSTBOK</li>
<li>OM MIG</li>
<li>PORTFOLIO</li>
<li>CURRICULUM VITAE</li>
</ul>
I wont get any errors, But when Im trying with the other code I get alot of errors. Is there any way to write the code so It validates ?
This is the code thas bugging me
<?php
$index = 'menu';
$gb = 'menu';
$portfolio = 'menu';
$cv = 'menu';
$menuLinkid=basename($_SERVER['PHP_SELF'],'.php');
if($menuLinkid=='index'){
$index = 'myButtons';
}else if($menuLinkid=='gb'){
$gb ='myButtons';
}else if($menuLinkid=='portfolio'){
$portfolio ='myButtons';
}else if($menuLinkid=='cv'){
$cv ='myButtons';
}
?>
<div id="fronticon">
<a href="contact.php"><img src="images/em.png" alt="Email" title="Email"/>
</a>
</div>
<ul>
<li><a class="<?php echo $index; ?> "href="index.php">OM MIG</a></li>
<li><a class="<?php echo $gb; ?> "href="gb.php">GÄSTBOK</a></li>
<li><a class="<?php echo $portfolio;?> " href="portfolio.php">PORTFOLIO</a></li>
<li><a class="<?php echo $cv; ?> "href="cv.php">CURRICULUM VITAE</a></li>
</ul>
Thanks
You need spaces between the ?> " and the href. You can't have them next to each other with no space.
Should be:
<li><a class="<?php echo $index; ?> " href="index.php">OM MIG</a></li>

Creating Dynamic Links with PHP/MySQL

i'm creating my first PHP/MySQL site and i'm having difficulty figuring out how to generate dynamic links and creating a new page for those links.
My index page is pulling in certain details from my database as a preview, and when the visitor clicks on that item, i want them to be taken to a page which shows the full information from the database for that row.
The code on my index page for displaying the previews is below, any help on amending it to generate the link and page would be greatly appreciated.
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle"><?php echo $f2; ?></li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
</div>
<?php
$i++;
}
?>
I'm pretty sure what i'm asking is really simple, i just can't get my head around acheieving it.
Thanks to you both for your answers, but i have managed to fix it (or work-around it) with this on my index page:
<?php
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
do {?>
<div class = "hjl"><a href="paging.php?job_id=<?php echo $rsjobinfo['job_id'];?>">
<ul>
<li id = "jobtitle"><?php echo $rsjobinfo['job_title'];?></li><br />
<li id = "compname"><?php echo $rsjobinfo['company_name'];?></li>
</ul>
<ul>
<li id = "city"><?php echo $rsjobinfo['city'];?>,
<?php echo $rsjobinfo['country'];?></li>
</ul>
</a>
</div>
<?php } while ($rsjobinfo=mysql_fetch_assoc($result))?>
</div>
Followed by this on my content page:
<?php
$job_id = $_GET['job_id'];
$query="SELECT * FROM $tbl_name WHERE job_id = $job_id";
$result=mysql_query($query) or die(mysql_error());
$rsjobinfo=mysql_fetch_assoc($result);
mysql_close();
?>
Thanks for your help everyone.
Dan
put mysql_close after you use mysql_result, but once you get it working you might look into a more modern approach like PDO.
to your code add link (which I think you already have somewhere):
//...................
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li>
//...................
Read more...
//...................
then your code must check for variable $_GET['id'], so put IF in the beginning of your code:
$where = '';
if( isset($_GET['id']) && strlen($_GET['id']) > 0 ) {
$where = ' job_id = "'. mysql_real_escape_string( $_GET['id'] ) .'"' ;
}
<?php
$query="SELECT * FROM $tbl_name $where ORDER BY job_id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"company_name");
$f2=mysql_result($result,$i,"job_title");
$f3=mysql_result($result,$i,"city");
$f4=mysql_result($result,$i,"country");
$job_id=mysql_result($result,$i,"job_id");
?>
<div class = "hjl">
<ul>
<li id = "jobtitle">
<a href="<?php echo '?id='.$job_id; ?>">
<?php echo $f2; ?>
</a>
</li><br />
<li id = "compname"><?php echo $f1; ?></li>
</ul>
<ul>
<li id = "city"><?php echo $f3; ?>, <?php echo $f4; ?></li><br />
</ul>
Read more...
</div>
<?php
$i++;
}
?>
edit: Try changing the following line:
$where = " job_id = '". mysql_real_escape_string( $_GET['id'] ) ."'" ;

Categories