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.
Related
How do I change my URL's function for calling up on pages?
Here's an example: https://exaplesitename.com/index.php?id=21
What it looks like now but I want it to be something like this: https://exaplesitename.com/Entertainment
This is the navigation code:
<?php
function find_parent_categories($options=[]) {
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Parent_Categories ";
if($Active){
$sql .= "WHERE Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
}
function find_categories($id_Category_Parent, $options=[]){
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Categories ";
$sql .= "WHERE id_Category_Parent='" . db_escape($db, $id_Category_Parent) . "' ";
if($Active){
$sql .= "AND Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
} ?>
<nav class="navbar fixed-top navbar-expand-lg bg-white navbar-custom">
<a class="navbar-brand" href="<?php echo url_for('/index.php'); ?>">
<img src="../../images/Logo1.png" alt="Nantucket" class="img-fixed"/>
</a>
<button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">Menu
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
<?php $nav_categories = find_parent_categories(['Active' => true]); ?>
<ul class="navbar-nav mr-auto">
<?php while($nav_category = mysqli_fetch_assoc($nav_categories)) { ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="<?php echo url_for('index.php?id=' . h(u($nav_category['id']))); ?>" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo h($nav_category['Category_Name']); ?></a>
<?php $nav_sub_categories = find_categories($nav_category[id], ['Active' => true]); ?>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown1">
<?php while($nav_sub_category = mysqli_fetch_assoc($nav_sub_categories)) { ?>
<li>
<a class="dropdown-item" href="<?php echo url_for('index.php?id=' . h(u($nav_sub_category['id']))); ?>"> <?php echo h($nav_sub_category['Category_Name']); ?></a>
</li>
<?php } //while $nav_sub_categories ?>
</ul>
<?php mysqli_free_result($nav_sub_categories); ?>
<?php mysqli_free_result($nav_categories); ?>
</li>
<?php } ?>
</ul>
</div>
</nav>
Thank you very much for any help and kindness.
Any change in url can be achieved by changing .htaccess file.
RewriteEngine On
RewriteRule https://exaplesitename.com/Entertainment https://exaplesitename.com/index.php?id=21
For more information about .htaccess please refer to:
https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/
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
}
}
?>
I have a WHILE loop to create a set of 4 tabs with tab titles I get from the DB. I also have a field named tab_id (1,2,3,4) inside the DB.
When I run this code the tab_id is "1" for all 4 rows...but the tab titles display correctly. What am I doing wrong?
<?php
while($rows = mysqli_fetch_array($result)){
if ($rows['tab_id'] = 1) { ?>
<li class="active"><a href="#tab<? echo $rows['tab_id']; ?>"
role="tab" data-toggle="tab" ><? echo $rows['tab_title']; ?></a>
</li>
<?php } else { ?>
<li><a href="#tab<? echo $rows['tab_id']; ?>" role="tab" data-
toggle="tab" ><? echo $rows['tab_title']; ?></a></li>
<?php }} ?>
A single = is an assignment operator in php. So php thinks you are saying "Declare $rows['tab_id'] as 1, does 1 loosely evaluate as true?" ...the answer is "yes", so the condition always returns true. (This is the same logic occurring in the while loop on the previous line.)
To make a loose comparison (that is all that is needed) you merely need to add one more = to make ==.
Further advice is that your snippet can be written more D.R.Y. (Don't Repeat Yourself). You are only modifying class="active" so there is no need to rewrite the entire <li> tag.
Suggested code:
while($rows = mysqli_fetch_array($result)){
<li<?php if($rows['tab_id'] == 1) echo ' class="active"'; ?>><a href="#tab<? echo $rows['tab_id']; ?>"
role="tab" data-toggle="tab" ><? echo $rows['tab_title']; ?></a>
</li>
<?php } ?>
or
while($rows = mysqli_fetch_array($result)){
echo "<li",($rows['tab_id'] == 1 ? ' class="active"' : ''),">";
echo "<a href=\"#tab{$rows['tab_id']}\" role=\"tab\" data-toggle=\"tab\" >{$rows['tab_title']}</a>";
echo "</li>";
}
<?php
while($rows = mysqli_fetch_array($result)){
if ($rows['tab_id'] ==1) { ?>
<li class="active"><a href="#tab<? echo $rows['tab_id']; ?>"
role="tab" data-toggle="tab" ><? echo $rows['tab_title']; ?></a>
</li>
<?php } else { ?>
<li><a href="#tab<? echo $rows['tab_id']; ?>" role="tab" data-
toggle="tab" ><? echo $rows['tab_title']; ?></a></li>
I have a php/mysql driven menu. I am trying to add an active class to the menu item whenever the URI reflects that menu item (basically, when I am on that page) but every example I find is with a menu that is not populated through an array or repeater like mine.
How can manipulate the active class you see below so that it shows only when I am on that page?
Here is my menu
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['id'] == 2){
// below is the active class that is now being populated accross all items equally
echo '<li class="nav-item dropdown active">';
echo '<a href="#" class="nav-link dropdown-toggle animated fadeIn animation-delay-7" data-toggle="dropdown" data-hover="dropdown" role="button" aria-haspopup="true" aria-expanded="false" data-name="services">';
echo $row["pagename"];
echo '<i class="zmdi zmdi-chevron-down"></i>';
echo '</a>';
// create a new list
echo '<ul class="dropdown-menu">';
// loop second/submenu query results
while($sub_row = $result2->fetch_assoc()) {
echo '<li>';
echo '' . $sub_row["pagename"] . '';
echo '</li>';
}
echo "</ul>";
echo '</li>';
} else { // for all other menu links
echo '<li class="nav-item dropdown active">';
echo '' . $row["pagename"] . '';
echo '</li>';
}
}
} else {
echo "0 results";
}
?>
The screenshot below shows the result after it renders. Notice the active class highlighted in yellow, it is added to every item in my main menu (the way I have it coded, which it's wrong of course). The green is the $sub_row["link"] (to answer Mark's question below
Notice, the active class is only needed to be added programmatically on the main menu. (the else portion of my code).
The screenshot below shows how the active class affects the menu item if the user was in the www.domain.com/how-it-works.php page. The active class contians the styles to make the menu item appear that way.
Thanks in advance
Hard to test with not having the DB outputs to match against mate but try this, basically you want an if statement to get the current page URL and match it against the url you get back from the DB.
<?php
if ($result->num_rows > 0) {
$pageURL = str_replace('/', '', $_SERVER['REQUEST_URI']);
$homeURL = $_SERVER['REQUEST_URI'];
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['id'] == 2){ ?>
<li class="nav-item dropdown">
<a href="" class="nav-link dropdown-toggle animated fadeIn animation-delay-7" data-toggle="dropdown" data-hover="dropdown" role="button" aria-haspopup="true" aria-expanded="false" data-name="services">
<?php echo $row["pagename"];?>
<i class="zmdi zmdi-chevron-down"></i>
</a>
<ul class="dropdown-menu">
<?php while($sub_row = $result2->fetch_assoc()) {?>
<li>
<a href="<?php echo $sub_row["link"]; ?>" class="dropdown-item">
<?php echo $sub_row["pagename"] ?>
</a>
</li>
<?php } ?>
</ul>
</li>
<?php
} else if($row['id'] == 1){ ?>
<li class="nav-item dropdown <?php echo ($homeURL == $row["link"])? 'active' : '';?>">
<a href="<?php echo $row["link"]; ?>" class="nav-link dropdown-toggle animated fadeIn animation-delay-7">
<?php echo $row["pagename"]; ?>
</a>
</li>
<?php
} else { ?>
<li class="nav-item dropdown <?php echo ($pageURL == $row["link"])? 'active' : '';?>">
<a href="<?php echo $row["link"]; ?>" class="nav-link dropdown-toggle animated fadeIn animation-delay-7">
<?php echo $row["pagename"]; ?>
</a>
</li>
<?php }
}
} else {
echo "0 results";
}
?>
Edit:
Bit that gets the current page url is:
$pageURL = end(explode('/', $_SERVER['REQUEST_URI']));
Bit that does the check is (this is just a shorthand if statement:
<?php echo ($pageURL == $row["link"])? 'active' : '';?>
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.