Doesen't show Data - php

I'm trying to select and display some info about a record from my database.
After selecting the user/name/record and pressing Afiseaza (submit), the page remains the same, like not refreshing.
I can't find the problem. Here is my code:
<?php
include 'ch20_include.php';
doDB();
if('!_POST') {
//Arata
$display_block = "<h1>Alege un Elev</h1>";
//Primeste partile
$get_list_sql = "SELECT id,CONCAT_WS(', ', nume,p_nume) AS display_name FROM nume_principal ORDER BY p_nume,nume";
$get_list_res = mysqli_query($mysqli,$get_list_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rowS($get_list_res) < 1) {
//Nici un elev
$display_block .= "<p><em>Scuze,nu a fost selectat nici un Elev!</em></p>";
} else {
//Afiseaza
$display_block .= "
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\"/>
<p><label for=\"sel_id\">Alege un Elev:</label><br/>
<select id='sel_id\' name=\"sel_id\" required=\"required\">
<option value=\"\">--Alege--</option>";
while ($recs = mysqli_fetch_array($get_list_res)) {
$id = $recs['id'];
$display_name = stripslashes($recs['display_name']);
$display_block .= "<option value=\"".$id."\">".$display_name."</option>";
}
$display_block .= " </select> <button type=\"submit\" name=\"submit\" value=\"view\">Afiseaza\"></button>
</form> ";
}
//rezultate
mysqli_free_result($get_list_res);
} else if($_POST) {
//Verifica field
if ($_POST['sel_id'] == "") {
header("Location: seldata.php");
exit;
}
//Versiune sigura ID
$safe_id = mysqli_real_escape_string($mysqli, $_POST['sel_id']);
$get_nume_sql = "SELECT concat_ws(' ',p_nume,nume) as display_name FROM nume_principal WHERE id = '".$safe_id."'";
$get_nume_res = mysqli_query($mysqli, $get_nume_sql) or die(mysqli_error($mysqli));
while ($nmae_info = mysqli_fetch_array($get_nume_res)) {
$display_name = stripslashes($name_info['display_name']);
}
$display_block = "<h1>Arata Informatile pentru ".$display_name."</h1>";
mysqli_free_result($get_nume_res);
//Adresa
$get_adresele_sql = "SELECT adresa,oras,judet,cod_postal FROM adress WHERE nume_id = '".$safe_id."'";
$get_adresele_res = mysqli_query($mysqli, $get_adresele_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_adresele_res) > 0) {
$display_block .= "<p><strong>Adresa:</strong><br/><ul>";
while ($add_info = mysqli_fetch_array($get_adresele_Res)) {
$adresa = stripslashes($add_info['adresa']);
$oras = stripslashes($add_info['oras']);
$judet = stripslashes($add_info['oras']);
$cod_postal = stripslashes($add_info['cod_postal']);
$display_block .= "<li>$adresa,$oras,$judet,$cod_postal</li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_adresele_res);
//telefon
$get_tel_sql = "SELECT tel_numar,type FROM telefon WHERE nume_id = '".$safe_id."'";
$get_tel_res = mysqli_query($mysqli, $get_tel_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_tek_res) > 0) {
$display_block .= "<p><strong>Telefon:</stron><br/> <ul>";
while ($tel_info = mysqli_fetch_array($get_tel_res)) {
$tel_numar = stripslashes($tel_info['tel_numar']);
$tel_type = $tel_info['type'];
}
$display_block .= "</ul>";
}
mysqli_free_result($get_tel_res);
//Adresele toate
$get_biologie_sql = "SELECT notab_1, notab_2, notab_3, notab_4, notab_5, medieb FROM biologie WHERE nume_id = '".$safe_id."'";
$get_biologie_res = mysqli_query($mysqli, $get_biologie_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_biologie_res) > 0 ) {
$display_block .= "<p><strong>Note-Biologie:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_biologie_res)) {
$notab_1 = stripslashes($add_info['notab_1']);
$notab_2 = stripslashes($add_info['notab_2']);
$notab_3 = stripslashes($add_info['notab_3']);
$notab_4 = stripslashes($add_info['notab_4']);
$notab_5 = stripslashes($add_info['notab_5']);
$medieb = stripslashes($add_info['medieb']);
$display_block .= "<li>$notab_1 $notab_2 $notab_3 $notab_4 $notab_5 $medieb </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_biologie_res);
$get_chimie_sql = "SELECT notac_1, notac_2, notac_3, notac_4, notac_5, mediec FROM chimie WHERE nume_id = '".$safe_id."'";
$get_chimie_res = mysqli_query($mysqli, $get_chimie_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_chimie_res) > 0 ) {
$display_block .= "<p><strong>Note-Chimie:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_chimie_res)) {
$notac_1 = stripslashes($add_info['notac_1']);
$notac_2 = stripslashes($add_info['notac_2']);
$notac_3 = stripslashes($add_info['notac_3']);
$notac_4 = stripslashes($add_info['notac_4']);
$notac_5 = stripslashes($add_info['notac_5']);
$mediec = stripslashes($add_info['mediec']);
$display_block .= "<li>$notac_1 $notac_2 $notac_3 $notac_4 $notac_5 $mediec </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_chimie_res);
$get_fizica_sql = "SELECT notaf_1, notaf_2, notaf_3, notaf_4, notaf_5, medief FROM fizica WHERE nume_id = '".$safe_id."'";
$get_fizica_res = mysqli_query($mysqli, $get_fizica_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_fizica_res) > 0 ) {
$display_block .= "<p><strong>Note-Fizica:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_fizica_res)) {
$notaf_1 = stripslashes($add_info['notaf_1']);
$notaf_2 = stripslashes($add_info['notaf_2']);
$notaf_3 = stripslashes($add_info['notaf_3']);
$notaf_4 = stripslashes($add_info['notaf_4']);
$notaf_5 = stripslashes($add_info['notaf_5']);
$medief = stripslashes($add_info['medief']);
$display_block .= "<li>$notaf_1 $notaf_2 $notaf_3 $notaf_4 $notaf_5 $medief </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_fizica_res);
$get_franceza_sql = "SELECT notafr_1, notafr_2, notafr_3, notafr_4, notafr_5, mediefr FROM franceza WHERE nume_id = '".$safe_id."'";
$get_franceza_res = mysqli_query($mysqli, $get_franceza_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_franceza_res) > 0 ) {
$display_block .= "<p><strong>Note-Franceza:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_franceza_res)) {
$notafr_1 = stripslashes($add_info['notafr_1']);
$notafr_2 = stripslashes($add_info['notafr_2']);
$notafr_3 = stripslashes($add_info['notafr_3']);
$notafr_4 = stripslashes($add_info['notafr_4']);
$notafr_5 = stripslashes($add_info['notafr_5']);
$mediefr = stripslashes($add_info['mediefr']);
$display_block .= "<li>$notafr_1 $notafr_2 $notafr_3 $notafr_4 $notafr_5 $mediefr </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_franceza_res);
$get_germana_sql = "SELECT notag_1, notag_2, notag_3, notag_4, notag_5, medieg FROM germana WHERE nume_id = '".$safe_id."'";
$get_germana_res = mysqli_query($mysqli, $get_germana_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_germana_res) > 0 ) {
$display_block .= "<p><strong>Note-Germana:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_germana_res)) {
$notag_1 = stripslashes($add_info['notag_1']);
$notag_2 = stripslashes($add_info['notag_2']);
$notag_3 = stripslashes($add_info['notag_3']);
$notag_4 = stripslashes($add_info['notag_4']);
$notag_5 = stripslashes($add_info['notag_5']);
$medieg = stripslashes($add_info['medieb']);
$display_block .= "<li>$notag_1 $notag_2 $notag_3 $notag_4 $notag_5 $medieg </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_germana_res);
$get_informatica_sql = "SELECT notai_1, notai_2, notai_3, notai_4, notai_5, mediei FROM informatica WHERE nume_id = '".$safe_id."'";
$get_informatica_res = mysqli_query($mysqli, $get_informatica_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_informatica_res) > 0 ) {
$display_block .= "<p><strong>Note-Informatica:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_informatica_res)) {
$notai_1 = stripslashes($add_info['notai_1']);
$notai_2 = stripslashes($add_info['notai_2']);
$notai_3 = stripslashes($add_info['notai_3']);
$notai_4 = stripslashes($add_info['notai_4']);
$notai_5 = stripslashes($add_info['notai_5']);
$mediei = stripslashes($add_info['mediei']);
$display_block .= "<li>$notai_1 $notai_2 $notai_3 $notai_4 $notai_5 $mediei </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_matematica_res);
$get_matematica_sql = "SELECT notam_1, notam_2, notam_3, notam_4, notam_5, mediem FROM matematica WHERE nume_id = '".$safe_id."'";
$get_matematica_res = mysqli_query($mysqli, $get_matematica_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_matemtica_res) > 0 ) {
$display_block .= "<p><strong>Note-Matematica:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_matematica_res)) {
$notam_1 = stripslashes($add_info['notam_1']);
$notam_2 = stripslashes($add_info['notam_2']);
$notam_3 = stripslashes($add_info['notam_3']);
$notam_4 = stripslashes($add_info['notam_4']);
$notam_5 = stripslashes($add_info['notam_5']);
$mediem = stripslashes($add_info['mediem']);
$display_block .= "<li>$notam_1 $notam_2 $notam_3 $notam_4 $notam_5 $mediem </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_matematica_res);
$get_romana_sql = "SELECT notar_1, notar_2, notar_3, notar_4, notar_5, medier FROM romana WHERE nume_id = '".$safe_id."'";
$get_romana_res = mysqli_query($mysqli, $get_romana_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_romana_res) > 0 ) {
$display_block .= "<p><strong>Note-Romana:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_romana_res)) {
$notar_1 = stripslashes($add_info['notar_1']);
$notar_2 = stripslashes($add_info['notar_2']);
$notar_3 = stripslashes($add_info['notar_3']);
$notar_4 = stripslashes($add_info['notar_4']);
$notar_5 = stripslashes($add_info['notar_5']);
$medier = stripslashes($add_info['medier']);
$display_block .= "<li>$notar_1 $notar_2 $notar_3 $notar_4 $notar_5 $medier </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_romana_res);
$get_religie_sql = "SELECT notare_1, notare_2, notare_3, notare_4, notare_5, mediere FROM religie WHERE nume_id = '".$safe_id."'";
$get_religie_res = mysqli_query($mysqli, $get_religie_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_religie_res) > 0 ) {
$display_block .= "<p><strong>Note-Religie:</strong><br/> <ul>";
while ($add_info = mysqli_fetch_array($get_religie_res)) {
$notare_1 = stripslashes($add_info['notare_1']);
$notare_2 = stripslashes($add_info['notare_2']);
$notare_3 = stripslashes($add_info['notare_3']);
$notare_4 = stripslashes($add_info['notare_4']);
$notare_5 = stripslashes($add_info['notare_5']);
$mediere = stripslashes($add_info['medieb']);
$display_block .= "<li>$notare_1 $notare_2 $notare_3 $notare_4 $notare_5 $mediere </li>";
}
$display_block .= "</ul>";
}
mysqli_free_result($get_religie_res);
//Nota personala
$get_notes_sql = "SELECT note FROM nota_personala WHERE nume_id = '".$safe_id."'";
$get_notes_res = mysqli_query($mysqli, $get_notes_sql) or die(mysqli_error($mysqli));
if (mysqli_num_rows($get_notes_res) == 1) {
while ($note_info = mysqli_fetch_array($get_notes_res)) {
$note = n12br(stripslashes($note_info['note']));
}
$display_block .= "<p><strong>Nota personala:</strong><br/> $note</p>";
}
mysqli_free_result($get_notes_res);
$display_block .= "<br/>
<p style=\"text-align:center\">
Alege altul</p>";
}
mysqli_close($mysqli);
?>
<!DOCTYPE html>
<html>
<head>
<title>Elevii</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>

First your code is wrong !_POST can not use like a string because
"if return true for any non zero element"
, So change it
if(!$_POST) or if($_SERVER['REQUEST_METHOD']!='POST')

Related

Change numeric array keys into $variable

I was wondering if anyone could help with a conceptual issue I'm having, or if there is a better way.
Basically I am searching my database for an array of terms through a search bar.
I have a function that loops through each term and then uses that for a search. It puts everything in an array and returns the array that is then processed into a table.
The search works fine, I put in the terms and it loops through them pushing the rows into an array. Now because I am pushing into different Arrays, the parent Array I Have has indexed keys, I want to change that to be keys of the terms and in the array there are two other arrays 1 for the number of results and another for the results.
// Search index php
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div id="search">
<form id= "search" action="searchresults.php" method="POST">
<input type="text" name="txtsearch" size="30" id="txtsearch">
<input type="submit" name="srchbtn" id="srchbtn">Search</button>
</form>
</body>
</html>
//Search PHP
<?php
include_once "iud.php";
$arrayval = array();
if(isset($_POST['txtsearch'])){
$search=$_POST['txtsearch'];
}else{
$search = '';
}
$search1 = explode(" ", $search);
array_push($arrayval, $search1);
$combined = array();
foreach ($arrayval as $val){
$orIndex = checkOr($val);
if(empty($orIndex)){
$valcntr = count($val);
$results = getResults($val);
print_r($results);
}
}
function getResults($array){
include_once "iud.php";
$db = connectDatabase();
$totalResults = array();
$length = count($array);
for ($i = 0; $i < $length; $i++){
$outputDisplay = "";
$myrowcount = 0;
$sql_statement = "SELECT userid, username, firstname, lastname ";
//, congroup, cattype, company, position, email, website, phone, mphone, wphone, fax, add1, add2, city, state, zip, country, reference, entrydate, enteredby, notes ";
$sql_statement .= "FROM userlist ";
$sql_statement .= "WHERE (firstname LIKE '%$array[$i]%' or lastname LIKE '%$array[$i]%')";
$sql_statement .= "ORDER BY lastname, firstname";
$sqlResults = selectResults($db, $sql_statement);
$error_or_rows = $sqlResults['resultNum'];
if (substr($error_or_rows, 0 , 5) == 'ERROR')
{
$outputDisplay .= "<br />Error on DB";
$outputDisplay .= $error_or_rows;
} else {
array_push($totalResults, $sqlResults);
$arraySize = $error_or_rows;
}
}
return ($totalResults);
}
function buildTable($totalResults){
include_once "iud.php";
$outputDisplay = "";
$outputDisplay .= '<div id="resultstable">';
$outputDisplay .= '<ul id="results">';
$myrowcount = 0;
for ($i=0; $i < count($totalResults); $i++)
{
$myrowcount++;
$contactid = $totalResults[$i]['userid'];
$firstname = $totalResults[$i]['firstname'];
$lastname = $totalResults[$i]['lastname'];
$pcat = $totalResults[$i]['username'];
$outputDisplay .= '<li id='.$contactid.' class="indRes" ';
$outputDisplay .= 'name="'.$contactid.'" >';
$outputDisplay .= '<span style="font-size:10px;cursor:pointer" onclick="getContactsToForm();">';
$outputDisplay .= '<h3>'.$firstname.' ' .$lastname. '</h3>';
$outputDisplay .= '</span>';
$outputDisplay .= '</li>';
}
$outputDisplay .= '</ul>';
$outputDisplay .= "</div>";
return $outputDisplay;
}
function checkOr($searchArray){
if (in_array("OR",$searchArray)){
$orPos = array_search("OR", $searchArray);
if (!empty($orPos)){
$surrVal = "";
$surrVal = --$orPos;
$orValArray = array();
array_push($orValArray,$searchArray[$surrVal]);
$surrVal = $orPos+2;
array_push($orValArray,$searchArray[$surrVal]);
return $orValArray;
}
}
}
?>
//common functinos IUD
<?php
function connectDatabase()
{
$db = mysqli_connect('localhost','root','');
if (!$db)
{
print "<h1>Unable to Connect to mysqli</h1>";
}
$dbname = 'test';
$btest = mysqli_select_db($db, $dbname);
if (!$btest)
{
print "<h1>Unable to Select the Database</h1>";
}
return $db;
}
function selectResults($db, $statement)
{
$output = "";
$outputArray = array();
$db = connectDatabase();
if ($db)
{
$result = mysqli_query($db, $statement);
if (!$result) {
$output .= "ERROR";
$output .= "<br /><font color=red>mysqli No: ".mysqli_errno();
$output .= "<br />mysqli Error: ".mysqli_error();
$output .= "<br />SQL Statement: ".$statement;
$output .= "<br />mysqli Affected Rows: ".mysqli_affected_rows()."</font><br />";
array_push($outputArray, $output);
} else {
$numresults = mysqli_num_rows($result);
$outputArray['resultNum'] = $numresults;
for ($i = 0; $i < $numresults; $i++)
{
$row = mysqli_fetch_array($result);
$outputArray["rowArray"] = $row;
}
}
} else {
array_push($outputArray, 'ERROR-No DB Connection');
}
return $outputArray;
}
function iduResults($db, $statement)
{
$output = "";
$outputArray = array();
$db = connectDatabase();
if ($db)
{
$result = mysqli_query($db, $statement);
if (!$result) {
$output .= "ERROR";
$output .= "<br /><font color=red>mysqli No: ".mysqli_errno();
$output .= "<br />mysqli Error: ".mysqli_error();
$output .= "<br />SQL Statement: ".$statement;
$output .= "<br />mysqli Affected Rows: ".mysqli_affected_rows()."</font><br />";
} else {
$output = mysqli_affected_rows();
}
} else {
$output = 'ERROR-No DB Connection';
}
return $output;
}
?>
My search returns an array that when printed looks like this:
Array(
[0]=>Array
(
[resultNum]=>1
[rowArray]=>Array(
[0]=>1
[userid]=>1
etc
)
)
[1]=>Array
(
[resultNum]=>1
[rowArray]=>Array(
[0]=>2
[userid]=>2
etc
)
)
I want to make the two outer arrays' keys [0],[1] into the search terms that are used in the loop through the getResults($array) function.
Any ideas?
Change
array_push($totalResults, $sqlResults);
to:
$totalResults[$array[$i]] = $sqlResults;
to make it an associative array instead of an indexed array.
P.S. you should learn to use foreach to loop over array elements, and use prepared statements to prevent SQL injection.
Also, selectResults() is only returning the last row of the query. It should be:
} else {
$numresults = mysqli_num_rows($result);
$outputArray['resultNum'] = $numresults;
$outputArray['rowArray'] = array();
for ($i = 0; $i < $numresults; $i++)
{
$row = mysqli_fetch_array($result);
array_push($outputArray["rowArray"], $row);
}
so that it returns all the rows instead of overwriting rowArray each time through the loop.

Update form with two file fields

I cannot get my two file upload fields working with my update form. I'm able to get create_form to upload the files to my server and input info into the SQL database, but I can't get the edit to take without receiving an error. Files don't upload and info doesn't update in SQL. Please help!
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php
session_start();
if($_SESSION["login_user"] != true) {
echo("Access denied!");
exit();
}
?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/validation_functions.php");?>
<?php find_selected_event_page(); ?>
<?php
if (!$current_event) {
// page ID was missing or invalid or
// page couldn't be found in database
redirect_to("manage_content.php");
}
?>
<?php
if (isset($_POST['submit'])) {
// Process the form
// validations
$required_fields = array("visible");
validate_presences($required_fields);
if (empty($errors)) {
// Perform Update
$id = $current_event["id"];
$visible = mysql_prep($_POST["visible"]);
$homepage = mysql_prep($_POST["homepage"]);
$fa_id = mysql_prep($_POST["fa_id"]);
$title = mysql_prep($_POST["title"]);
$caption = mysql_prep($_POST["caption"]);
$url = mysql_prep($_POST["url"]);
$month = mysql_prep($_POST["month"]);
$date = mysql_prep($_POST["date"]);
$year = mysql_prep($_POST["year"]);
$summary = mysql_prep($_POST["summary"]);
$full_text = mysql_prep($_POST["full_text"]);
$image = rand(1000,100000)."-".$_FILES['image']['name'];
$image_loc = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_type = $_FILES['image']['type'];
$image_folder="images/";
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_folder="files/";
$final_image=str_replace(' ','-',$new_image_name);
$final_file=str_replace(' ','-',$new_file_name);
if($_FILES) {
unlink("images/".$current_event['image']);
move_uploaded_file($image_loc,$image_folder.$final_image);
unlink("files/".$current_event['file']);
move_uploaded_file($file_loc,$file_folder.$final_file); }
else
{
// if no image selected the old image remain as it is.
$final_image = $current_event['image']; // old image from database
$fine_file = $current_event['file']; // old image from database
}
$query = "UPDATE `events` SET ";
$query .= "`visible` = '{$visible}', ";
$query .= "`homepage` = '{$homepage}', ";
$query .= "`fa_id` = '{$fa_id}', ";
$query .= "`title` = '{$title}', ";
$query .= "`caption` = '{$caption}', ";
$query .= "`url` = '{$url}', ";
$query .= "`month` = '{$month}', ";
$query .= "`date` = '{$date}', ";
$query .= "`year` = '{$year}', ";
$query .= "`summary` = '{$summary}', ";
$query .= "`full_text` = '{$full_text}', ";
$query .= "`image` = '{$final_image}', ";
$query .= "`image_type` = '{$image_type}', ";
$query .= "`image_size` = '{$image_new_size}' ";
$query .= "`file` = '{$final_file}', ";
$query .= "`file_type` = '{$file_type}', ";
$query .= "`file_size` = '{$file_new_size}' ";
$query .= "WHERE `events`.`id` = {$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection)) {
// Success
echo "<pre>".$query."</pre>";
$_SESSION["message"] = "Item updated.";
redirect_to("manage_content.php");
} else {
// Failure
//$_SESSION["message"] = "Item creation failed.";
//redirect_to("new_news.php");
echo "Error: " . $query . "<br>" . $result->error;
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
The error I get is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'file = '', file_type = '', file_size = '' WHERE events.id = 1 LIMIT 1' at line 1
update
$query .= "`image_size` = '{$image_new_size}' ";
to
$query .= "`image_size` = '{$image_new_size}' ,";
so your final query
$query = "UPDATE `events` SET ";
$query .= "`visible` = '{$visible}', ";
$query .= "`homepage` = '{$homepage}', ";
$query .= "`fa_id` = '{$fa_id}', ";
$query .= "`title` = '{$title}', ";
$query .= "`caption` = '{$caption}', ";
$query .= "`url` = '{$url}', ";
$query .= "`month` = '{$month}', ";
$query .= "`date` = '{$date}', ";
$query .= "`year` = '{$year}', ";
$query .= "`summary` = '{$summary}', ";
$query .= "`full_text` = '{$full_text}', ";
$query .= "`image` = '{$final_image}', ";
$query .= "`image_type` = '{$image_type}', ";
$query .= "`image_size` = '{$image_new_size}', ";
$query .= "`file` = '{$final_file}', ";
$query .= "`file_type` = '{$file_type}', ";
$query .= "`file_size` = '{$file_new_size}' ";
$query .= "WHERE `events`.`id` = {$id} ";
$query .= "LIMIT 1";

Each frontoffice can only view their users - PHP

I need help in something.
On the site where I'm working I have the option to search users, but when we search users we can see every user no matter the frontoffice and I need help for each user see only users that belong to the same frontoffice. I tried the following code:
if ($row->idFrontOfficeSinalizador == $idFrontOfficeSinalizador)
Where the $row->idFrontOfficeSinalizador is the users frontoffice id and the $idFrontOfficeSinalizador is my frontoffice id
This is all the code that I have
include('importarBibliotecas.php');
if (!isset($_SESSION['id']) || $_SESSION['idTiposDePermissoes'] == 3 )
echo "<script>window.location='index.php'</script>";
?>
<script src='scriptFormUtentes.js'></script>
<script>
function makeDivVisible(){
document.getElementById('encaminharUtentes').style.display='block';
}
function makeDivInvisible(){
document.getElementById('encaminharUtentes').style.display='none';
}
function makeDivEdInvisible(){
document.getElementById('editarEncaminharUtentes').style.display='none';
}
</script>
<?php
include('menu.php');
include('gerirUtentesFormEditarUtente.php');
include('gerirUtentesFormEditarEncaminhamento.php');
include('gerirUtentesPOSTEditarUtente.php');
if (isset($_POST['NIFS']) || isset($_GET['n'])){ //pesquisar utentes por NIF
include_once('DataAccess.php');
$da = new DataAccess();
if (isset($_POST['NIFS'])){
$nif = $_POST['NIFS'];
$nome = $_POST['Nome'];
$idFrontOfficeSinalizador = $_POST['frontoffice'];
$emailTecnico = $_POST['email'];
$interesseProfissional = $_POST['interesseProfissional'];
$escolaridade = $_POST['Escolaridade'];
$situacaoEmprego = $_POST['situacaoEmprego'];
$estado = $_POST['estado'];
$res = $da->getUtentes($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $escolaridade, $situacaoEmprego, $estado);
//PU = Pesquisa de Utentes
$_SESSION['PU_nif'] = $nif;
$_SESSION['PU_nome'] = $nome;
$_SESSION['PU_idFrontOfficeSinalizador'] = $idFrontOfficeSinalizador;
$_SESSION['PU_emailTecnico'] = $emailTecnico;
$_SESSION['PU_interesseProfissional'] = $interesseProfissional;
$_SESSION['PU_escolaridade'] = $escolaridade;
$_SESSION['PU_situacaoEmprego'] = $situacaoEmprego;
$_SESSION['PU_estado'] = $estado;
$numPaginas = mysql_num_rows($res)/15;
$numPaginas = ceil ($numPaginas);
$_SESSION['PU_numPaginas'] = $numPaginas;
$pagAtual = $_GET['pg'];
$res = $da->getUtentesPorPagina($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $escolaridade, $situacaoEmprego, $estado, $pagAtual);
//echo "<script>alert('".mysql_num_rows($res)." $numPaginas')</script>";
}else{
$nif = $_GET['n'];
$res = $da->getUtenteNIF($nif);
}
}else{
if (isset($_GET['pg'])){
$nif = $_SESSION['PU_nif'];
$_POST['NIF'] = $nif;
$nome = $_SESSION['PU_nome'];
$_POST['Nome'] = $nome;
$idFrontOfficeSinalizador = $_SESSION['PU_idFrontOfficeSinalizador'];
$_POST['frontoffice'] = $idFrontOfficeSinalizador;
$emailTecnico = $_SESSION['PU_emailTecnico'];
$_POST['email'] = $emailTecnico;
$interesseProfissional = $_SESSION['PU_interesseProfissional'];
$_POST['interesseProfissional'] = $interesseProfissional;
$escolaridade = $_SESSION['PU_escolaridade'];
$_POST['Escolaridade'] = $escolaridade;
$situacaoEmprego = $_SESSION['PU_situacaoEmprego'];
$_POST['situacaoEmprego'] = $situacaoEmprego;
$estado = $_SESSION['PU_estado'];
$_POST['estado'] = $estado;
$res = $da->getUtentesPorPagina($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional,
$escolaridade, $situacaoEmprego, $estado, $_GET['pg']);
if ( mysql_num_rows($res) == 0){ //se a página não devolver resultados, mostra a primeira página
echo "<script>window.location='gerirUtentes.php?pg=1'</script>";
}
}
//pesquisar utente depois de clicar em Encaminhar
if (isset($_GET['f'])){
include_once('DataAccess.php');
$da = new DataAccess();
$res = $da->getUtente($_GET['f']);
}
//inserir encaminhamento
if (isset($_POST['buttonInserirEncaminhamentoUtente'])){
$idUtente = $_POST['idUtente'];
$idTecnico = $_POST['idTecnico'];
$data = $_POST['data'];
$texto = $_POST['observacoes'];
include_once('DataAccess.php');
$da = new DataAccess();
$da->inserirEncaminhamento($data, $texto, $idTecnico, $idUtente);
echo "<script>alert('Diligência inserida com sucesso')</script>";
}else{
//editar encaminhamento
if (isset($_POST['buttonEditarEncaminhamento'])){
$id = $_POST['id'];
$idUtente = $_POST['edit_idUtente'];
$idTecnico = $_POST['edit_idTecnico'];
$data = $_POST['edit_data'];
$texto = $_POST['edit_observacoes'];
include_once('DataAccess.php');
$da = new DataAccess();
$da->editarEncaminhamento($id, $data, $texto, $idTecnico, $idUtente);
echo "<script>alert('Diligência editada com sucesso')</script>";
}
}
}
include('gerirUtentesFormPesquisa.php');
if (isset($_POST['NIFS']) || isset($_GET['f']) || isset($_GET['n'])){
echo "<div class='ink-grid'>
<table style='width:100%' class='ink-table'>
<thead>
<tr>
<th style='width:25%' align='left'>Nome</th>
<th style='width:15%' align='left'>Pedido Inicial</th>
<th style='width:10%' align='left'>Situação</th>
<th style='width:15%' align='left'>Habilitações</th>
<th style='width:20%' align='left'>Interesse Profissional</th>
<th style='width:10%' align='left'>FrontOffice</th>
<th style='width:15%'></th>
</tr>
</thead>
<tbody>";
$numPaginas = $_SESSION['PU_numPaginas'];
if ($numPaginas > 1){
$next = $_GET['pg'] + 1;
if ($next == $numPaginas)
$next = 1;
if ($_GET['pg'] == 1)
$before = $numPaginas;
else
$before = $_GET['pg']-1;
echo "<tr>
<td><a href='gerirUtentes.php?pg=$before' class='button'>Pág. Anterior</a></td>
<td colspan='4'> </td>
<td align='right'><a href='gerirUtentes.php?pg=$next' class='button'>Pág. Seguinte</a></td>
</tr>";
}
while($row = mysql_fetch_object($res)){
if ($row->interesseProfissional1 != -1)
$nomeIP = $da->getInteresseProfissional($row->interesseProfissional1);
else
$nomeIP = "---";
if ($row->idFrontOfficeSinalizador != -1)
$nomeFO = $da->getFrontOfficeName($row->idFrontOfficeSinalizador);
else
$nomeFO = "---";
if ($row->idHabilitacoes != -1)
$Habilitacao = $da->getHabilitacao($row->idHabilitacoes);
else
$Habilitacao = "---";
$situacaoProfissional="";
switch ($row->empregado){
case 1: $situacaoProfissional = "Empregado";
break;
case 0: $situacaoProfissional = "Desempregado";
break;
}
if ($row->Estudante == 1){
if ($situacaoProfissional != "") $situacaoProfissional .= ", ";
$situacaoProfissional .= "Estudante";
}
if ($row->outraSituacao == 1){
if ($situacaoProfissional != "") $situacaoProfissional .= ", ";
$situacaoProfissional .= "Outra Situação";
}
if($situacaoProfissional == "") $situacaoProfissional="---";
$pedidoInicial = "";
if($row->pedidoInicialEmprego == 1)
$pedidoInicial = "Emprego";
if($row->pedidoInicialFormacao == 1){
if ($pedidoInicial != "")
$pedidoInicial .= ", Formação";
else
$pedidoInicial = "Formação";
}
if($row->pedidoInicialOutra == 1){
if ($pedidoInicial != "")
$pedidoInicial .= ", Outra";
else
$pedidoInicial = "Outra";
}
echo "<tr>
<td>
<a href='gerirUtentes.php?i=$row->a&f=$row->a' title='Detalhes do utente'><img src='img/info.png' style='width:25px'/>
<font color='black'>$row->nome</font>
</a>
</td>
<td>$pedidoInicial</td>
<td>$situacaoProfissional</td>
<td>$Habilitacao</td>
<td>$nomeIP</td>
<td>$nomeFO</td>
<td align='right'>
";
if ($row->Email != "")
echo "<a href='enviarEmail.php?i=$row->id' target='_blank'><img title='Enviar E-mail para utente' src='img/mail.png' style='width:20px'/></a> ";
$numEncaminhamentos = $da->getNumEncaminhamentos($row->a);
if ($numEncaminhamentos >0)
echo "<a href='gerirUtentes.php?f=$row->a' style='text-decoration: none;'>
<img title='Diligências efetuadas' src='img/forward.png' style='width:20px'/>
<font size='1'>$numEncaminhamentos</font>
</a>";
else
echo "<a href='gerirUtentes.php?f=$row->a'>
<img title='Diligências efetuadas' src='img/forward.png' style='width:20px'/>
</a>
<br/>";
if ($row->CV != "")
echo "<a href='CVs/$row->CV' target='_blank'><img title='Download do CV' src='img/cv.png' style='width:20px'/></a> ";
//download informações para pdf
echo "<a href='PDF.php?i=$row->a' target='_blank'>
<img title='Download das informações pessoais' src='img/pdf.png' style='width:24px'/>
</a>";
echo "
<a href='gerirUtentes.php?d=$row->a' onclick='return confirmarApagarUtente()'><img title='Apagar utente' src='img/delete3.png' style='width:20px'/></a>
</td>
</tr>";
}
if ($numPaginas > 1){
echo "<tr>
<td><a href='gerirUtentes.php?pg=$before' class='button'>Pág. Anterior</a></td>
<td colspan='4'> </td>
<td align='right'><a href='gerirUtentes.php?pg=$next' class='button'>Pág. Seguinte</a></td>
</tr>";
}
echo "</tbody>
</table>
</div>";
}
if (isset($_GET['d'])) {
include_once('DataAccess.php');
$da = new DataAccess();
$da -> deleteUtente($_GET['d']);
echo"
<script>alert('Utente eliminado com sucesso.');</script>";
}
include('gerirUtentesResultadosEncaminhamento.php');
if(isset($_GET['i'])){
include_once('DataAccess.php');
$da = new DataAccess();
$res = $da->getUtente($_GET['i']);
//ver campos do utente!!
verFormEditarUtente();
}
include('footer.php');
?>
</body>
</html>
DataAcces ->
function getUtentes($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $Escolaridade, $situacaoEmprego,$estado){
$this->connect();
$idTecnico = -1;
if ($emailTecnico != "")
{
$query = "select id from tecnicos where email = '$emailTecnico'";
$res = $this->execute($query);
if (mysql_num_rows($res)>0){
$row = mysql_fetch_array($res);
$idTecnico = $row[0];
}
}
$query = "select *, U.id as a from utentes U where estado=$estado ";
if ($nome != "")
$query .= " and nome like '%$nome%' ";
if ($idFrontOfficeSinalizador != "" && $idFrontOfficeSinalizador != "-1")
$query .= " and idFrontOfficeSinalizador = $idFrontOfficeSinalizador ";
if ($nif != "")
$query .= " and NIF = $nif ";
if ($interesseProfissional != -1 && $interesseProfissional != "")
$query .= " and (interesseProfissional1 = $interesseProfissional or interesseProfissional2 = $interesseProfissional or interesseProfissional3 = $interesseProfissional) ";
if ($Escolaridade != -1 && $Escolaridade != "")
$query .= " and idHabilitacoes = $Escolaridade ";
switch ($situacaoEmprego)
{
case 1:
$query .= " and empregado = 1";
break;
case 2:
$query .= " and empregado = 0";
break;
case 3:
$query .= " and Estudante = 1";
break;
case 4:
$query .= " and outraSituacao = 1";
break;
default:
break;
}
if ($idTecnico != -1)
$query .= " and idTecnico = $idTecnico ";
$query .= " order by U.nome asc ";
//echo $query;
$res = $this->execute($query);
$this->disconnect();
return $res;
}
function getUtentesPorPagina($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $Escolaridade,
$situacaoEmprego,$estado, $pagina){
$this->connect();
$idTecnico = -1;
if ($emailTecnico != "")
{
$query = "select id from tecnicos where email = '$emailTecnico'";
$res = $this->execute($query);
if (mysql_num_rows($res)>0){
$row = mysql_fetch_array($res);
$idTecnico = $row[0];
}
}
$query = "select *, U.id as a from utentes U where estado=$estado ";
if ($nome != "")
$query .= " and nome like '%$nome%' ";
if ($idFrontOfficeSinalizador != "" && $idFrontOfficeSinalizador != "-1")
$query .= " and idFrontOfficeSinalizador = $idFrontOfficeSinalizador ";
if ($nif != "")
$query .= " and NIF = $nif ";
if ($interesseProfissional != -1 && $interesseProfissional != "")
$query .= " and (interesseProfissional1 = $interesseProfissional or interesseProfissional2 = $interesseProfissional or interesseProfissional3 = $interesseProfissional) ";
if ($Escolaridade != -1 && $Escolaridade != "")
$query .= " and idHabilitacoes = $Escolaridade ";
switch ($situacaoEmprego)
{
case 1:
$query .= " and empregado = 1";
break;
case 2:
$query .= " and empregado = 0";
break;
case 3:
$query .= " and Estudante = 1";
break;
case 4:
$query .= " and outraSituacao = 1";
break;
default:
break;
}
if ($idTecnico != -1)
$query .= " and idTecnico = $idTecnico ";
$query .= " order by U.nome asc ";
$queryAux = $query;
$pagina = ($pagina-1) * 15;
$query .= " limit $pagina, 15 ";
$res = $this->execute($query);
/*
if ( mysql_num_rows($res) == 0){ //se a página não devolver resultados, mostra a primeira página
$pagina = 0;
$queryAux .= " limit $pagina, 15 ";
$res = $this->execute($queryAux);
}*/
$this->disconnect();
return $res;
}
function getUtenteNIF($nif){
$query = "select *, U.id as a from utentes U
where U.NIF = $nif";
$this->connect();
$res = $this->execute($query);
$this->disconnect();
return $res;
}
Whatever your query is, add a new condition to it for that in WHERE clause
AND idFrontOfficeSinalizador = 4 // use correct variable here

how to use foreach inside while loop?

Currently I am using following code to get data sorted by starting letter of name, if you run this code you will get what i am trying to create
<?php
$dirs = array('Aname1','Aname2','Aname3','A Nmae','Bname ','Cname','Cardiff','Dname','Dname',);
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo "<li class=\"title\">".$cur_let."</li>";
}
echo "<li class=\"clear\">
<div class=\"name\">".$dir."</div>
<div class=\"mobile\"></div>
<div class=\"telephone\"></div>
<div class=\"email\"></div>
<div class=\"action\">edit | delete</div>
<div class=\"clear\"></div>
</li>";
}
but how to use above loop inside following to get vales from database and it should be display like (I want highlight first letter) http://i.stack.imgur.com/bLHVD.jpg
$query = "SELECT * FROM phone_number";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$names = $row['name'].",";
}
?>
With MySQLi:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
With mysql_* deprecated functions:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
Try something like this:
$query = "SELECT * FROM phone_number ORDER BY name DESC";
$result = mysql_query($query) or die(mysql_error());
$lastLetter = '';
$html = '<ul>';
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
if (strtoupper($name[0]) !== $lastLetter) {
if ($lastLetter !== '')
$html .= '</ul></li>';
$lastLetter = strtoupper($name[0]);
$html .= '<li class="title">' . $lastLetter;
$html .= '<ul>';
}
$html .= '<li>' . $name . '</li>';
}
$html .= '</ul></li></ul>';

PHP unitialized string offset: 0

I decided to give PHP a try, and then bought lynda.com's essential training tutorial.
The problem is, that I get this error:
( ! ) Notice: Uninitialized string offset: 0 in
C:\wamp\www\widget_corp\includes\functions.php on line 147.
when trying to compare two values.
Can anyone help me ? :)
error in navigation function:
if($page["id"] == $selectedPage['id']){
Class content.php below:
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php");?>
<?php findSelectedPage(); ?>
<?php include("includes/header.php");?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($selSubject, $selectedPage);?>
<br/>
+ Add a new subject
</td>
<td id="page">
<?php echo checkSubjOrPage();?>
<br/>
<div id="footer">Copyright 2007, Widget Corp</div>
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>
class function.php below:
<?php
//This file is the place to store all basic functions.
//NB!
//function to prevent problems with submitting values, that contains
//chars such as: "", '' etc., into the database.
function mysql_prep($value){
$magic_quotes_active = get_magic_quotes_gpc;
//i.e. php>= v4.3.0
$new_enough_php = function_exists("mysql_real_escape_string");
if($new_enough_php){
//undo any magic quotes effects so mysql_real_escape_string can do the work
if($magic_quotes_active){
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
//if magic quotes aren't already on then add slashed manually
if(!$magic_quotes_active){
$value = addslashes($value);
}
}
return $value;
}
function confirm_query($result_set){
if(!$result_set){
die("Database connection failed: " . mysql_error());
}
}
function getAllSubjects(){
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}
function getPagesForSubject($subject_id){
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}
function get_subject_by_id($subject_id){
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//REMEMBER:
//if no rows are returned, fetch_array will return false.
if($subject = mysql_fetch_array($result_set)){
return $subject;
} else {
return NULL;
}
}
function get_page_by_id($page_id){
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//REMEMBER:
//if no rows are returned, fetch_array will return false.
if($page = mysql_fetch_array($result_set)){
return $page;
} else {
return NULL;
}
}
function checkSubjOrPage(){
global $selSubject;
global $selectedPage;
if(!is_null($selSubject)){
return "<h2>" . $selSubject['menu_name'] . "</h2>";
} else if(!is_null($selectedPage)){
return "<h2>" . $selectedPage['menu_name'] . "</h2>" . "<div>" . $selectedPage['content'] . "</div>";
} else {
return "<h2>" . "Select a subject or page to edit!" . "</h2>";
}
}
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = "";
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
function navigation($selSubject, $selectedPage){
$output = "<ul class=\"subjects\">";
//3. Perform our database query
$subject_set = getAllSubjects();
while($subject = mysql_fetch_array($subject_set)){
$output .= "<li";
if($subject["id"] == $selSubject['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = getPagesForSubject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)){
$output .= "<li";
if($page["id"] == $selectedPage['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?page=" . urlencode($page{"id"}) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
function getPositions(){
$subject_set = getAllSubjects();
$subject_counts = mysql_num_rows($subject_set);
$output = "<select name=\"position\">";
//$subject_counts + 1 b/c we are adding a subject.
for($count = 1; $count <= $subject_counts +1; $count++){
$output .= "<option value=\"{$count}\">{$count}</option>";
}
return $output . " </select>";
}
?>
here you override $selectedPage to a string or set it to null
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = "";
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
And here it should be an array:
if($page["id"] == $selectedPage['id']){
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = NULL;
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
function navigation($selSubject, $selectedPage){
$output = "<ul class=\"subjects\">";
//3. Perform our database query
$subject_set = getAllSubjects();
while($subject = mysql_fetch_array($subject_set)){
$output .= "<li";
if(isset($selSubject['id']) && $subject["id"] == $selSubject['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = getPagesForSubject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)){
$output .= "<li";
if(isset($selectedPage['id']) && $page["id"] == $selectedPage['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?page=" . urlencode($page{"id"}) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}

Categories