Changing a recursive condition in a SQL/PHP website menu - php

I am modifying a site built in PHP with content drawn from a SQL database. The original developer had the navigation created with the following code. It creates the navigation from the CMS pages - it works fine for top level pages but I am trying to modify the way subpages are presented.
What I want it to do is when a subpage is present, it first opens a DIV containing formatting, then populates the subpagemenu by using the 'while' statement below. Finally, when the subpagemenu is finished, it closes the DIV.
I can't seem to work out which condition 'opens' the subpage menu before it goes through the loop of filling out the subpage menu.
Any and all help appreciated - thanks!
<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";
}
}
}
}
}
}
}
?>

<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
echo "<div class='submenu'>"; //you can either give class or id whatever you want
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";}
}
echo "</div>";
}
}
}
}
}
?>
added the tag where it is requrired

Related

how to display a single database row after clicking on row from a iterated list in php?

I have a if statement that goes through all of the title options and all of them are a link to the same page. The idea is that depending on which title option is clicked on the linked page will show the title in h3 tags.
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
$item =$row['title'];
echo "<div>
<a href='p&fproject.php' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
The last if statement was an attempt to identify the link that was clicked on by id instead of title but it doesn't work.
Page linked to:
<?php
include 'header1.php';
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
for ($i = 0; $i < $queryResults; $i++) {
$item = $row['title'];
echo "<div>
<a href='' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
You can easily do that passing the post id in GET. What you want to do is put in the href your link project.php followed by a question mark with the id of the post, like this: 'project.php?id='" . $row["id"] . ", and then, in the page linked to, you can access that variable by doing postId = $_GET["id"] and making an SQL query on that id

paginate the list in php

i have this php code that fetches images from the database using the userid, then displays all the images in a list form. im trying to paginate the images, in a limit of 5 items per page. but the code is showing only the first page, without a link to the other pages. here's my php code
<?php
include 'connect.php';
$Category = " ";
$query = "SELECT Img_dir, Caption, Category FROM images WHERE Category = '". $_REQUEST['Category'] ."' AND user_id = '". $_SESSION['user_id'] ."' LIMIT 0,5";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result)){
$image = $row["Img_dir"];
$Caption= $row["Caption"];
$Category = $row["Category"];
echo "<dl>";
echo "<dd>$Category &nbsp&nbsp <img src='base64_encode($image)' />&nbsp&nbsp $Caption<dd>";
echo "</dl>";
}
//number of total pages available
$results_per_page = 10;
$number_of_results = mysqli_num_rows($result);
echo $number_of_pages = ceil($number_of_results / $results_per_page);
echo "<br>"; echo "<br>";
for($r=1;$r<=$number_of_pages;$r++)
{
?><?php echo $r." "; ?><?php
}
?>
You can try this:
Change your query (use prepare statments):
$query = "SELECT Img_dir, Category FROM images WHERE user_id = ? AND Category = ? ";
As for the structure of your data.
$results = [];
while ($row = $result->fetch_assoc()){
$key = $row['Category'];
if(!isset($results[$key])) $results[$key] = [];
$results[$key][] = $row['Img_dir ']; //['Category' => [Img_dir,Img_dir, ...]]
}
And your HTML. I would use a description list or dl as it has a nice place for the title:
foreach($results as $Category => $image){
echo "<dl>";
echo "<dt>$Category</dt>";
foreach($data as $row){
echo "<dd><img src='base64_encode($image)' /><dd>";
}
echo "</dl>";
}
Untested.
The order will probably be all wanky, so you can use ksort on it. Simply
ksort($results);
Before the foreach loops.
Cheers.

Display a new div after every number of records fetch from database in MySQL

I want to create a div for ads on a project I'm working on. I want the div to show after 8 records is fetch from the database.
My select statement:
$query = mysql_query("SELECT * FROM posts WHERE `username` = '$followe' OR `user_id` = ' $get_id' ORDER BY date_added DESC LIMIT 15 ");
$newsCount = mysql_num_rows($query); // Count the output amount
if ($newsCount > 0) {
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}
}
You need to add a counter for your while loop and echo the HTML string when the counter is divisible by 8:
<?php
$query = mysql_query("SELECT * FROM posts WHERE `username` = '$followe' OR `user_id` = ' $get_id' ORDER BY date_added DESC LIMIT 15 ");
$newsCount = mysql_num_rows($query); // Count the output amount
if ($newsCount > 0) {
$rowcount=1;
$divhtml = "<div></div>";
while ($row = mysql_fetch_assoc($query)){
if ($rowcount % 8 == 0) {
echo $divhtml;
}
$rowcount++;
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}
}
?>
You should change the content of variable $divhtml, with your own ad HTML.
Use an accumulator pattern such as this.
$i = 0
while($row = mysql_fetch_assoc($query)){
$i++;
if(i == 8) {
// DO WHATEVER
}
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}

mysqli_fetch_array reading result as null?

Any idea why this is showing a result of null? I'm guessing it has to do with where I put $link before I did the query, but it has to be done that way, correct?
function getcatposts($cat_id) {
$qc = #mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
}
$qc2 = #mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
}
Updated code:
function getcatposts($cat_id) {
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
$qc = mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
}
echo $cat_name;
echo '<br />';
echo $topic_title;
echo '<br />';
echo $topic_id;
}
New issue is that its displaying like this:
http://gyazo.com/43e8a91b9e0cf4f5e413536907891dcf.png
When the DB looks like this:
http://gyazo.com/1ead8bd0f150838dae3ee4a476419679.png
It should be displaying all three of them and this is a function meaning it will keep redoing all the code until it can't query anymore data. Any ideas?
The problem here is that you're trying to echo the values outside your loop. The variables inside the loop will get overwritten on each iteration and at the end of looping, the variable will hold the value of the last iteration.
If you want to display all the values, move the echo statement inside your loop, like so:
while($row = mysqli_fetch_array($qc))
{
$topic_title = $row['topic_subject'];
$topic_id = $row['topic_id'];
echo $topic_title.'<br/>';
echo $topic_id.'<br/>';
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2))
{
$cat_name = $row2['cat_name'];
echo $cat_name.'<br/>';
}
If you care about the order, you could store the titles, ids and cat_names in arrays like so:
while($row = mysqli_fetch_array($qc))
{
$topic_title[] =$row['topic_subject'];
$topic_id[] = $row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2))
{
$cat_name[] =$row2['cat_name'];
}
And then loop through them:
for ($i=0; $i < count($topic_id); $i++) {
if( isset($topic_id[$i], $topic_title[$i], $cat_name[$i]) )
{
echo $cat_name[$i].'<br/>';
echo $topic_title[$i].'<br/>';
echo $topic_id[$i].'<br/>';
}
}
Your function displays only one result because your echo is outside the while loop...
Put the Echo statements inside the loop, or you will print only the last result!
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
echo $topic_title;
echo '<br />';
echo $topic_id;
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
echo $cat_name;
echo '<br />';
}

PHP first active button

I have a little question for you!
I make a selection from mysql my buttons for example:
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
echo '$row['name']';
}
so I call all the navigation from mysql and I have a class in css which is called as .active, this class make a active button when I click it, but how can I make the first button active?
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
if ($row['id'] == $_GET['id'])) {
echo '<a class="active" href="?id=$row['id']">$row['name']</a>';
} else {
echo '$row['name']';
}
}
Try this.
<?PHP
$query = mysql_query("select * from navigation");
$first = true;
while($row = mysql_fetch_assoc($query)) {
if ((!array_key_exists('id', $_GET) && $first) || $row['id'] == $_GET['id']) {
$extra = 'class="active"';
$first = false;
} else
$extra = '';
echo "<a $extra href=\"?id={$row['id']}\">{$row['name']}</a>";
}
?>

Categories