I have been trying to find information on passing variables without a form using url, but I can't seem to find anythig that pertains to my situation.
Basically I need to have a page that is hierachical in the sense that you can click one course out of a list, that brings you to a list of sections, and you can click on a section which will bring to a page with the correct information for that one section. I was taught for this situation to use $_SESSION and then to include the variables in the url. My problem is that everytime I click on a course or a section, the information for every single course or section that is a query match is displaying rather than just the information for that one course or section. This is what I have for each of the pages in hierarchical order (not including the connect portion of the code which works fine anyways). I know this is a ton of code, and a complete mess. I will keep searching out information, and many many thanks in advance for any help.
List of Courses
session_start();
$section = $_SESSION['section_id'];
$user_fname = $_SESSION['user_fname'];
$user_lname = $_SESSION['user_lname'];
$query = "SELECT DISTINCT course_t.course_id, course_name, user_fname, user_lname, section_t.section_id
FROM course_t, authorized_user_t, section_t, teaching_history_t
WHERE authorized_user_t.authorized_user_id = teaching_history_t.instructor_id
AND teaching_history_t.section_id = section_t.section_id
AND section_t.course_id = course_t.course_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<p align = "center"><strong>' .$line1['course_id'].':'.$line1['course_name']. '</strong><br><br>';
}
List of Sections (it is supposed to list a single intructor and then each of the sections they teach under their name)
session_start();
$section_id = $_REQUEST['section_id'];
$user_fname = $_REQUEST['user_fname'];
$user_lname = $_REQUEST['user_lname'];
$course_id = $_SESSION['course_id'];
$course_name = $_SESSION['course_name'];
$section_id = $_SESSION['section_id'];
$semester = $_SESSION['semester'];
$year = $_SESSION['year'];
$course_description = $_SESSION['course_description'];
$SGoal_Description = $_SESSION['SGoal_Description'];
$sobjective_description = $_SESSION['sobjective_description'];
$LObjective_Description = $_SESSION['LObjective_Description'];
$topic_title = $_SESSION['topic_title'];
$topic_description = $_SESSION['topic_description'];
$coursework_title = $_SESSION['coursework_title'];
$coursework_location = $_SESSION['coursework_location'];
$coursework_description = $_SESSION['coursework_description'];
$query = "SELECT course_t.course_id, course_name, section_t.section_id, semester, year, course_description,
SGoal_Description, sobjective_description, LObjective_Description, topic_title, topic_description, coursework_title,
coursework_location, coursework_description, syllabus
FROM course_t, section_t, section_goal_section_t, section_goal_t, section_coursework_t,
mapped_topic_section_t, mapped_lis_section_t, coursework_t, topic_t, lis_objective_t, section_objective_t
WHERE course_t.course_id = section_t.course_id
AND section_t.section_id = section_goal_section_t.section_id
AND section_goal_section_t.sgoal_id = section_goal_t.sgoal_id
AND section_goal_section_t.section_id = mapped_lis_section_t.section_id
AND mapped_lis_section_t.lobjective_id = lis_objective_t.lobjective_id
AND section_t.section_id = mapped_topic_section_t.section_id
AND mapped_topic_section_t.topic_id = topic_t.topic_id
AND section_t.section_id = section_coursework_t.section_id
AND mapped_lis_section_t.sobjective_id = section_objective_t.sobjective_id
AND section_coursework_t.coursework_id = coursework_t.coursework_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<ul align = "center"<strong>' .'Instructor'. ':' ."$user_fname". "$user_lname".'<li align = "center">
<a href ="course_section.php?course_id='.$line1['course_id'].'&course_name='.$line1['course_name'].'§ion_id='.$line1['section_id'].'
&semester='.$line1['semester'].'&year='.$line1['year'].'&course_description='.$line1['course_description'].'
&SGoal_Description='.$line1['SGoal_Description'].'&sobjective_description='.$line1['sobjective_description'].'
&LObjective_Description='.$line1['LObjective_Description'].'&topic_title='.$line1['topic_title'].'
&topic_description='.$line1['topic_description'].'&coursework_title='.$line1['coursework_title'].'
&coursework_location='.$line1['coursework_location'].'&coursework_description='.$line1['coursework_description'].'">'.$line1['section_id'].'</a></strong><br><br>';
}
Section Information (should display information for the section that is clicked on)
session_start();
$topic_description = $SESSION['topic_description'];
$coursework_description = $SESSION['coursework_description'];
$course_id = $_REQUEST['course_id'];
$course_name = $_REQUEST['course_name'];
$section_id = $_REQUEST['section_id'];
$semester = $_REQUEST['semester'];
$year = $_REQUEST['year'];
$course_description = $_REQUEST['course_description'];
$SGoal_Description = $_REQUEST['SGoal_Description'];
$sobjective_description = $_REQUEST['sobjective_description'];
$LObjective_Description = $_REQUEST['LObjective_Description'];
$topic_title = $_REQUEST['topic_title'];
$topic_description = $_REQUEST['topic_description'];
$coursework_title = $_REQUEST['coursework_title'];
$coursework_location = $_REQUEST['coursework_location'];
$coursework_description = $_REQUEST['coursework_description'];
echo '<div align = "right">';
echo 'Return to List of Sections';
echo '</div>';
$query = "SELECT DISTINCT course_t.course_id, course_name, section_t.section_id, semester, year, course_description,
SGoal_Description, sobjective_description, LObjective_Description, topic_title, topic_description, coursework_title,
coursework_location, coursework_description, syllabus
FROM course_t, section_t, section_goal_section_t, section_goal_t, section_coursework_t,
mapped_topic_section_t, mapped_lis_section_t, coursework_t, topic_t, lis_objective_t, section_objective_t
WHERE course_t.course_id = section_t.course_id
AND section_t.section_id = section_goal_section_t.section_id
AND section_goal_section_t.sgoal_id = section_goal_t.sgoal_id
AND section_goal_section_t.section_id = mapped_lis_section_t.section_id
AND mapped_lis_section_t.lobjective_id = lis_objective_t.lobjective_id
AND section_t.section_id = mapped_topic_section_t.section_id
AND mapped_topic_section_t.topic_id = topic_t.topic_id
AND section_t.section_id = section_coursework_t.section_id
AND mapped_lis_section_t.sobjective_id = section_objective_t.sobjective_id
AND section_coursework_t.coursework_id = coursework_t.coursework_id";
$result = #mysql_query($query);
while ($line1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<p align = "center"><strong>' .$line1['course_id'].':'.' '.$line1['course_name']. '</strong><br><br>';
echo '<p align = "center"><strong>' .$line1['section_id'].':'.' '.$line1['semester'].'/'.$line1['year']. '</strong><br><br>';
echo '<p align = "left"><strong>' .$line1['course_description'].'</strong><br><br><br><br>';
echo '<div align = "left">';
echo '<ul align = "left"><strong>Section Goals:</strong></ul><li align = "left">' .$line1['SGoal_Description']. '</li>';
echo '<ul align = "left"><strong>Section Objectives:</strong></ul><li align = "left">' .$line1['sobjective_description']. '</li>';
echo '<ul align = "left"><strong>Mapped Objectives:</strong></ul><li align = "left">' .$line1['LObjective_Description']. '</li>';
echo '<ul align = "left"><strong>'.'Topic'.':'.' '.$line1['topic_title']. '</strong></ul>
<li align = "left">Click for Topic Description';
echo '<ul align = "left"><strong>'.'Coursework'.':'.' '.$line1['coursework_title']. '</strong></ul>
<li align = "left">Click for Coursework Description';
}
Don't use sessions, just include it in the url ex: example.com/index.php?example=true. If you use $_GET['example'] it will have the value 'true'.
Related
I have a number pages namely
change=1.php
change=2.php
change=3.php
They all have similar coding but leaving 1 or 2 variable values.
And I know its a very bad idea! How can I make a link work like below:
change.php?id=1
change.php?id=2
change.php?id=3
http://oi62.tinypic.com/708gfm.jpg
<?php
include 'connection.php';
session_start();
include 'details.php';
/*$pkmn_id = $_SESSION['pkmn_id'];
$poke = $_SESSION['path'];*/
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE team = 1 AND user_id = '".$id."' ");
while($rows = mysql_fetch_array($data))
{
$rep_id = $rows[0];
$pkmn_id = $rows['pkmn_id'];
$path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
$poke = mysql_result($path, 0, "path");
echo $poke;
echo "<br />";
$level = $rows['level'];
echo $level;
echo "<br />";
$exp = $rows['exp'];
echo $exp;
echo "<br />";
echo "<br />";
}
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND team = 0");
while($rows = mysql_fetch_assoc($data))
{
$db_id = $rows['id'];
$array[] = $db_id;
$level = $rows['level'];
$array1[] = $level;
$exp = $rows['exp'];
$array2[] = $exp;
$pkmn_id = $rows['pkmn_id'];
$data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
while($rows = mysql_fetch_assoc($data1))
{
$poke = $rows['path'];
$array3[] = $poke;
}
}
$team = 1;
$_SESSION['team'] = $team;
$_SESSION['rep_id'] = $rep_id;
?>
My PHP code.
You probably want to use GET variables, for which you need to combine all the files into one, named change.php. In this file you need the line $foo = $_GET["id"] which will get the value of the variable "id" in the url change.php?id=1.
if (isset($_GET["id"])) {
$foo = $_GET["id"];
//your code here
}else{
echo 'ERROR!!! No id in URL';
}
You can have several variables in the URL like this: change.php?id=1&a=bar&b=toofoo
You can get current script's file name and parse integer.
__FILE__
gives current script's name. Then,
$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id
I have a function that retrieve list of friends from the MySQL table using the friend_array field.
But the problem is that the browser doesn't show more than one picture, but it shows the default one.
this is the code
<?php
//***********************Displaying Friend List*************************//
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
/*$frnd1 = $friendArray[0];
$frnd2 = $friendArray[1];
/*$frnd3 = $friendArray[2];
$frnd4 = $friendArray[3];
$friendList .='<div style="background-color:"#CCC";>'.$frnd1.'<br />'.$frnd2.'</div>';*/
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $key => $value)
{
$i++;
$check_pic = "members/$value/image01.jpg";
if(file_exists($check_pic))
{
$frnd_pic = '<img src = \"$check_pic\" width = "52px" border = "1"/>';
}
else
{
$frnd_pic = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList = '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
}
$friendList.='</div>';
}
?>
you images and divs that contain the records of friends is being replaced by next record so it display the last record of your friends.
try this may help you
$friendList = "";
$friendListTitle="";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
var_dump($friendCount);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
$i=0;
$friendList ='<div style="background-color:"#CCC"; >';
foreach($friendArray as $frndlist => $value)
{
$i++;
$check_pic = 'members/'.$value.'/image01.jpg';
if(file_exists($check_pic))
{
//storing each friend images separately
$frnd_pic[$i] = '<img src = "'.$check_pic.'" width = "52px" border = "1"/>';
}
else
{
$frnd_pic[$i] = '<img src = "members/0/image01.jpg" width = "52px" border = "1"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value'LIMIT 1") or die(mysql_error());
if ($row = mysql_fetch_array($sqlName, MYSQL_ASSOC))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic[$i].'</div>';
}
}
$friendList.='</div>';
}
i have a list of friend that i want to show on each profile page where the user is friend with them but the problem is that when i use firebug the system display one id and with a default piture
so i think the error is in the foreach loop can anyone help me ????
php code:
<?php
//***********************Displaying Friend List*************************//
$friendListTitle = "";
$friendList = "";
if($friend_array!="")
{
$friendArray = explode(",", $friend_array);
$friendArray = array_slice($friendArray,0,6);
$friendCount = count($friendArray);
var_dump($friendCount);
$friendListTitle = '<div class="title"> '.$username.'\'s Friends('.$friendCount.')</div>';
//iterating to retrieve what it's needed as values
/*$frnd1 = $friendArray[0];
$frnd2 = $friendArray[1];
/*$frnd3 = $friendArray[2];
$frnd4 = $friendArray[3];
$friendList .='<div style="background-color:"#CCC";>'.$frnd1.'<br />'.$frnd2.'</div>';*/
$i=0;
$friendList .='<div style="background-color:"#CCC"; >';
foreach($friendArray as $key => $value)
{
$i++;
$check_pic = "members/$value/image01.jpg";
if(file_exists($check_pic))
{
$frnd_pic = '<img src = \"$check_pic\" width = \"30px\"; border = \"1\"/>';
}
else
{
$frnd_pic = '<img src = "members/0/image01.jpg" width = \"30px\" border = \"1\"/> ';
}
$sqlName = mysql_query("SELECT first_name, last_name FROM members WHERE user_id= '$value' LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array($sqlName))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList = '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
}
$friendList .='</div>';
}
?>
Your html table structure is broken:
add closing bracket here <table border = "0" align="center" cellpadding="3", add td tags, put tr tags into loop
also, replace
if($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
with
while($row = mysql_fetch_array($sqlName,MYSQL_ASSOC))
You have missed one dot. Please look at this
while($row = mysql_fetch_array($sqlName))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
}
because the varibale's name ($friendList) you need to make some string concatenation not simple substitution.
If You use substitution then everythinh what you have collected in $friendList will be lost.
The main problem was in this line:
$friendList .= '<div title="'.$fname.' '.$lname.'">'.$frnd_pic.'</div>';
Hello im learning so im sorry if this is a fool question. (also sorry about my bad english)
Im trying to display the items from a particular category.
In my Database, i have set my categories like this.
And the Products or Items like this,
Im using this code to display tree categories.
function hasChild($parent_id)
{
$sql = "SELECT COUNT(*) as count FROM category WHERE parent_id = '" . $parent_id . "'";
$qry = mysql_query($sql);
$rs = mysql_fetch_array($qry);
return $rs['count'];
}
function CategoryTree($list,$parent,$append)
{
$list = '<li>'.$parent['name'].'</li>';
if (hasChild($parent['id'])) // check if the id has a child
{
$append++;
$list .= "<ul class='child child".$append."'>";
$sql = "SELECT * FROM category WHERE parent_id = '" . $parent['id'] . "'";
$qry = mysql_query($sql);
$child = mysql_fetch_array($qry);
do{
$list .= CategoryTree($list,$child,$append);
}while($child = mysql_fetch_array($qry));
$list .= "</ul>";
}
return $list;
}
function CategoryList()
{
$list = "";
$sql = "SELECT * FROM category WHERE (parent_id = 0 OR parent_id IS NULL)";
$qry = mysql_query($sql);
$parent = mysql_fetch_array($qry);
$mainlist = "<ul class='parent'>";
do{
$mainlist .= CategoryTree($list,$parent,$append = 0);
}while($parent = mysql_fetch_array($qry));
$list .= "</ul>";
return $mainlist;
}
But i cant find a good way to convert the categories in links, so each time a users clic one category i will display the items for that category..
What would be the best for that.
If you can point me in the right direction, some tutorial, or something, would be really , really great.
It sounds like you are looking for some sort of java tree viewer, like this example?
http://dftree.sourceforge.net/dftree/example.html
There are several open source options for this (I have no experience of the one I list above, I just use it as an example). I'm sure I have used a tree viewer in the past that took a list format like you are using - I suggest you have a search around sourceforge.
Your question is not foolish, but is quite long... and many of us are quite lazy :-)
Ok i solve this
This is the code for the page with the menu and categories
<?php
function hasChild($parent_id)
{
$sql = "SELECT COUNT(*) as count FROM category WHERE parent_id = '" . $parent_id . "'";
$qry = mysql_query($sql);
$rs = mysql_fetch_array($qry);
return $rs['count'];
}
function CategoryTree($list,$parent,$append)
{
$productpage = "index.php";
$list = '<li><a href='.$productpage.'?'.'cat='.$parent['id'].'>'.$parent['name'].'</a></li>';
if (hasChild($parent['id'])) // check if the id has a child
{
$append++;
$list .= "<ul class='child child".$append."'>";
$sql = "SELECT * FROM category WHERE parent_id = '" . $parent['id'] . "'";
$qry = mysql_query($sql);
$child = mysql_fetch_array($qry);
do{
$list .= CategoryTree($list,$child,$append);
}while($child = mysql_fetch_array($qry));
$list .= "</ul>";
}
return $list;
}
function CategoryList()
{
$list = "";
$sql = "SELECT * FROM category WHERE (parent_id = 0 OR parent_id IS NULL)";
$qry = mysql_query($sql);
$parent = mysql_fetch_array($qry);
$mainlist = "<ul class='parent'>";
do{
$mainlist .= CategoryTree($list,$parent,$append = 0);
}while($parent = mysql_fetch_array($qry));
$list .= "</ul>";
return $mainlist;
}
?>
And the code for the product page
<?php
$producto = mysql_escape_string($_GET['producto']);
$tablename = "productos";
$res = mysql_query("SELECT * FROM `$tablename` WHERE `id` = '$producto'");
$row = mysql_fetch_assoc($res);
$uid = $row["id"];
$titulo = $row["titulo"];
$descripcion = $row["descripcion"];
$modelo = $row["modelo"];
$img = $row["img"];
// echo the output to browser or compound an output variables here
echo "ID = $uid
<br />Titulo = $titulo
<br>Descripcion = $descripcion
<br>Modelo = $modelo
<br>Imagen = <img src=\"../admin/$img\" width=\"40px\" height=\"40px\" >
<hr />";
?>
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