Jump to content

[SOLVED] Please help, have been looking at the screen too long .. breadcrumbs


zolen

Recommended Posts

Hi first of all I know that this is basic stuff.. and the problem is probably obvious.

However I am just not getting the results I am looing for.

Could some kind person please have a look at the code below and tell me where I have gone wrong.

it is an include that should recurse through a table in mysql based on parent id.

 

<?php

$o= $this_page;

$query2 = "SELECT * FROM page WHERE pageid = ".$o;

$result = mysql_query($query2); //calling query in database

$num_results = mysql_num_rows($result); //simplifying function into a variable (number of rows)

$_SESSION['totalpages'] = $num_results;

$row = mysql_fetch_assoc($result);// fetching data in database per row

$i=$row['parentid'];

 

 

while ($i !='')//first page has no parent

{

$query = "SELECT * FROM page WHERE pageid = ".$i;//selects parent

$result = mysql_query($query); //calling query in database

$num_results = mysql_num_rows($result); //simplifying function into a variable (number of rows)

$row = mysql_fetch_assoc($result);// fetching data in database per row

$i=$row['pageid'];

$title=$row['title'];

 

$before='<input type="hidden" name="new_page" value="'; //I know  this can be done "all in one" but its simpler for trouble shooting

$after='"><a href="#" onClick="document.formName.submit()">';

$afterafter='</a> >> ';

$concat= $before .$i .$after .$title .$afterafter;

$BCarray[] = $concat;

}

$basicbc = array_reverse($BCarray);

echo $basicbc;

?>

 

</form>

Link to comment
Share on other sites

You use the same query before the while loop and in the loop. The both use the same id. I don't really understand what you are trying to do.

 

almost the same query.. the first one pulls the current pageid, the other the page id of the parent. You have to know what the current page is to get the parent.

about site...

The site uses a dynamic page where only the content changes based on a POST. the $_POST then changes the pageid for the current page which is a variable used in a query to get the content.

 

I need to create an include that recurses thru the DB based on parent id .. chuck all the parentids into an array then output it as <form> elements.

 

I am no expert and would not be supprised if there is a far simpler way to do this .. but after hours of googling I decided I would try myself.

Link to comment
Share on other sites

Ok, I really dont like to work with database query data and trying to deal with rows off a query, I like to put the queries into associative arrays and then this cleans up what you have.

 

use this for start, to then better deal with the output, let me know if you need further

 

<?php 
ob_start();
session_start();

//function to take a sql statement and return a multi-dimensional associative array of the results
function selectSQL($theQuery) {
$openlink = mysql_connect($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]) 	or die ("failed to connect to server: " . mysql_error());
mysql_select_db ($GLOBALS["db"]) or die ("failed to connect to database ($db): " . mysql_error());
$query = mysql_query($theQuery) or die ("Select query failed: " . mysql_error());

$array_counter = 0;
$return_array = array();
      while ($row = mysql_fetch_assoc ($query)) 
	{
	        foreach ($row as $key => $value) {
	            $return_array[$array_counter][$key] = $value;
	            }
	        $array_counter++;
	}
	mysql_free_result ($query);
      		return $return_array;

  mysql_close($openlink) or die ("couldn't clean up connection: " . mysql_error());
}

//variable for the function above
$dbhost = 'localhost';
$dbuser = 'your user name here';
$dbpass = 'your password here';
$db = 'database name here';

//your code implemented here
$o = $this_page;
$query1 = selectSQL("SELECT parentid FROM page WHERE pageid = ".$o);
$query2 = selectSQL("SELECT * FROM page WHERE pageid = ".$query1[0]['parentid']);

for ($i=0;$i < count($query2);$i += 1)
{
$BCarray[] = '<input type="hidden" name="new_page" value="'.$query2[$i]['pageid'].'"><a href="#" onClick="document.formName.submit()">'.$query2[$i]['title'].'</a>[/url] >> ';
}

?>

Link to comment
Share on other sites

Thanks BeninBlack. Thats a huge help. I have included it and have started di-secting what you have done (for learning)

I have also re-worked my code (see code below) for learning purposes. Two issues left on my code .. 1. the post data is sending from the text submit buttons (see javascript) and 2 I think I may be using the array reverse wrong.

 

<?php
echo '<form name="king" method="post" action"">';
$o= $_SESSION['this_page'];
$query2 = "SELECT * FROM page WHERE pageid = ".$o;//selects this page from db
$result = mysql_query($query2); //calling query in database
$num_results = mysql_num_rows($result); //simplifying function into a variable (number of rows)
$_SESSION['totalpages'] = $num_results;		
$row = mysql_fetch_assoc($result);// fetching data in database per row
$i=$row['parentid'];//sets i as the parentid of this page
while ($i !='')//loops untill the parentid is null ie. the first page
{
$query = "SELECT * FROM page WHERE pageid = ".$i;//selects parent
$result = mysql_query($query); //calling query in database
$num_results = mysql_num_rows($result); //simplifying function into a variable (number of rows)
$row = mysql_fetch_assoc($result);// fetching data in database per row
$i=$row['parentid'];
$title=$row['title'];
$before='<input type="hidden" name="new_page" value="';
$haha=$row['pageid'];
$after='"> <a href"" onClick="submit()">';
$afterafter='</a>';
$concat= $before .$haha .$after .$title .$afterafter;
$BCarray[] = $concat;
echo $concat;//added to test input to array
}
//$basicbc = array_reverse($BCarray);//test $concat
//echo $basicbc;//test $concat
?>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.