PHP/SQL doesn't fetch all children from parent [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I created this code for a dropdown navigation menu. I have 2 tables in my database, one for parentitems and the other one for childitems. The parents getting out correctly, but the childitems won't.
The problem is, I only get one parent and one child at a time, or I get totally nothing.
Thanks in advance!
My code:
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('navigation',$con);
$query="select * from nav";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$m_id=$row['m_id'];
$m_title=$row['m_title'];
$child_query="select * from nav_child where parent_id='$m_id'";
$run_child=mysql_query($child_query);
while($row_child=mysql_fetch_array($run_child)) {
$child_id=$row_child['nav_id'];
$child_title=$row_child['child_title'];
echo"<ul>
<li><a href='menu.php'>$m_title</a>
<ul>
<li><a href='menu.php'>$child_title</a></li>
</ul>
</li>
</ul>";
}
}
?>

You need to split your html
while(mainquery) {
echo '<ul>' <-----note the location
while (subquery) {
echo '<li>subquery 1 stuff</li>'
}
echo '</ul>' <-----note the location
}
You're outputting it entirely in your subquery section, so EVERY child row gets its own complete <ul><li>...</li></ul> tag set.

Related

Trying to make this entire line of code into a hyperlink [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
echo <a href = 'test.php'> "CategoryID: " . $row["CategoryID"]. " - Category Name: ".$row["CategoryName"]. </a> "<br>";
This is what i have an is not working properly.
This:
echo "<a href = 'test.php'>CategoryID: {$row['CategoryID']} - Category Name: {$row['CategoryName']}</a><br />";
I am using the { and } as they allow you to include an array in a string and ignore the concatenation which I find harder to read.
I find it funny that you can loop through a MySQL array but can't echo a simple string :P
Some links (teach a man to fish...):
W3Schools
PHP documentation
Codecademy
Tutorials Point
Try this:
<?php
$link = "";
$link = sprintf("<a href = 'test.php'>CategoryID: %d - Category Name: %s </a><br />", $row['CategoryID'], $row['CategoryName']);
echo $link;
?>
Assuming that $row['CategoryID'] is an integer and $row['CategoryName'] is a string.

Get query data without foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently I make my query like this:
<?php
function Data() {
$db = DB::getInstance();
$query = $db->query("SELECT * FROM users");
$results = $query->results();
return ($results);
}
$listdata = Data();
?>
And when I want to see my information I have something like this:
<div class="container">
<h1>Title</h1>
<hr>
<div class="row">
....
...
..
.
<?php foreach ($listdata as $v1) { ?>
<p><?=$v1->username?></p>
<?php } ?>
</div>
</div>
But if I want to put information see for example in the footer of the page I have to open a new foreach
As I can transform this code to open the query to the top of the page and simply llabar variables where I want without opening another foreach?
Even better would defeat the function, I just want to put the query and data wherever.
Thank you!
You could try echo $listdata[0]->column_name thou this will print only the first row......so to be precise use a where clause in your sql statment
Edit: It will print only the first index in the array that contains the Fetched Data

Display text on page with specific tag in wordpress [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display a text with a specific tag in wordpress.
Ex: I have 2 tags: car and boat, and 2 text: "I have car and i want one bike", "I have boat and i am happy"
I want to display in header specific text when exist tag.
I try using tem exist in header.php but display text in all pages, not just in page where exist tag.
Ex:
<?php
$boat = term_exists('boat', 'post_tag');
if ($boat !== 0 && $boat !== null) {
echo "I have boat and i am happy!";
}
?>
If you have only two tags this will do.
<?php
if (is_tag( 'boat' )) {
echo "I have boat and i am happy!";
} elseif (is_tag('car')) {
echo "I have a car";
}
?>

how to set li active class using php if clicked [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello I would like to set li active in PHP: if it's clickable, then it's active, if not then it's not active. I've tried several ways but nothing works. Here's my code :
<ul>
<?php
$sql = mysql_query("SELECT * FROM pages WHERE isRoot='1' ORDER BY pageID");
while ($row = mysql_fetch_object($sql))
{
echo "<li>$row->pageTitle </li>";
}
?>
</ul>
The easiest way is to us a GET parameter.
The link to your site than will be index.php?pageID=11 - in this case 11 stands for die pageID 11
in your while loop you have to check if this site is the active one
if($row->pageID == $_GET['pageID'])
{
//active li
echo "<li class="active">$row->pageTitle </li>";
}
else
{
//normal li
echo "<li>$row->pageTitle </li>";
}
You could do the same with PageTitle too

search results displaying the same thing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a search bar and a sql database with 3 movies in
Name:Star Wars: Description: Contains the word group
Name:301: Description: Contains the word group
Name:destroyer: Description: Contains the word group
BUT when ever I search for the word "group"
I only get one result usually:
if i search for group star wars will appear 3 time.
also i know its vulnerable to sql injection but at the moment that is not an issue
here is my php code:
if(!isset($_POST['search']))
{
header("Location:index.php");
}
$search_sql="SELECT * FROM php_item WHERE Name LIKE '%".$_POST['search']."%' OR Description LIKE '%".$_POST['search']."%'";
$search_query = mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)
{
$search_rs= mysql_fetch_assoc($search_query);
}
?>
</p>
<p> Search Results</p>
<?php
if(mysql_num_rows($search_query) != 0){
do{ ?>
<p>
<?php echo $search_rs['Name']; ?>
<?php }while($searchr_rs=mysql_fetch_assoc($search_query));
} else {
echo "No Results Found";
}
?>
If this is your code, there is a typo in the while. It should be $search_rs=mysql_fetch_assoc($search_query). You have an extra 'r'.
Good Luck!

Categories