Jump to content

[SOLVED] Calling variables


j05hr

Recommended Posts

I want to call this to my website but it doesn't display what I want it to,

 

<h5>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h5>

 

If I change the $sel_subject to $sel_page it works, but both are using the exact same names in the database and have exactly the same code in my functions.php

 

Can anyone help?

 

functions.php

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 subject_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 find_selected_page () {
	global $sel_subject;
	global $sel_page;
	if (isset($_GET['subj'])) {
		$sel_subject = get_subject_by_id($_GET['subject']);
		$sel_page = NULL;
	} elseif (isset($_GET['page'])) {
		$sel_subject = NULL;
		$sel_page = get_page_by_id($_GET['page']);
	} else {
		$sel_subject = NULL;
		$sel_page = NULL;
	}
}


function navigation($sel_subject = null, $sel_page = null) {  
	$subject_set = get_all_subjects();
	// 5. Use returned data
	while ($subject = mysql_fetch_array($subject_set)) { 

	echo "<div class=\"menu-name\">";  echo "<a href=\"edit_subject.php?page=" . urlencode($subject["id"]) .
		"\">{$subject["menu_name"]}</a>";  echo"</div>";
	echo "<div class=\"buying-text\">";  echo "{$subject["content"]}";  echo"</div>";

	echo "<div class=\"image\">"; echo"</div>";

	}
}

 

Thanks,

Josh

Link to comment
Share on other sites

I did show one line of code where it should display the variable.

 

Here is the whole page

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php find_selected_page();?>
<?php include("includes/header.php"); ?>
<div id="content">
	<h2> Buying Edit Page</h2>
	<br />
	<h5>Edit Subject: <?php echo $sel_subject ['menu_name']; ?></h5>
		<form action="edit_subject.php?page=<?php echo urlencode($sel_subject['id']);?>" method="post">
			<p>Subject name: 
				<input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" />
			</p>
			<p>Edit content:
				<textarea name="content" rows="5" cols="70"><?php echo $sel_subject['content']; ?> </textarea>
				</p>
			<p>Position: 
				<select name="position">
					<?php
						$subject_set = get_all_subjects();
						$subject_count = mysql_num_rows($page_set);
						// $subject_count + 1 b/c we are adding a subject
						for($count=1; $count <= $subject_count+1; $count++) {
							echo "<option value=\"{$count}\">{$count}</option>";
						}
					?>
				</select>
			</p>
			<p>Visible: 
				<input type="radio" name="visible" value="0"<?php if ($sel_page['visible'] ==0) { echo " checked"; }
				?> /> No
				 
				<input type="radio" name="visible" value="1"<?php if ($sel_page['visible'] ==1) { echo " checked"; } 
				?> /> Yes
			</p>
			<input type="submit" value="Add Subject" />
		</form>
		<br/> 
		<a href="content1.php">Cancel</a>

<?php


?>
</div>
<?php require("includes/footer.php"); ?>

Link to comment
Share on other sites

Hi joshr,

 

$sel_subject['menu_name'] will not display unless $_GET['subj'] is set, that's how your find_selected_page() function is written:

 

function find_selected_page () {
	global $sel_subject;
	global $sel_page;
	if (isset($_GET['subj'])) {
		$sel_subject = get_subject_by_id($_GET['subject']);
		$sel_page = NULL;
	} elseif (isset($_GET['page'])) {
		$sel_subject = NULL;
		$sel_page = get_page_by_id($_GET['page']);
	} else {
		$sel_subject = NULL;
		$sel_page = NULL;
	}
}

 

Is this set?  I can't see how from the code above unless it's being set from the URL this page is loading from.

Link to comment
Share on other sites

I'm not sure if it is or not, I'm really not very good with PHP (design is my strengths)

 

This is all of my functions.php

 

<?php
// This file is the place to store all basic functions

function mysql_prep( $value ) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
	if( $new_enough_php ) { // PHP v4.3.0 or higher
		// undo any magic quote 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 slashes manually
		if( !$magic_quotes_active ) { $value = addslashes( $value ); }
		// if magic quotes are active, then the slashes already exist
	}
	return $value;
}

function redirect_to( $location = NULL ) {
	if ($location != NULL) {
		header("Location: {$location}");
		exit;
	}
}

function confirm_query($result_set) {
	if (!$result_set) {
		die("Database query failed: " . mysql_error());
	}
}

function get_all_subjects() {
	global $connection;
	$query = "SELECT * 
			FROM subjects 
			ORDER BY position ASC";
	$subject_set = mysql_query($query, $connection);
	confirm_query($subject_set);
	return $subject_set;
}

function get_pages_for_subject($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 find_selected_page() {
	global $sel_subject;
	global $sel_page;
	if (isset($_GET['subj'])) {
		$sel_subject = get_subject_by_id($_GET['subj']);
		$sel_page = NULL;
	} elseif (isset($_GET['page'])) {
		$sel_subject = NULL;
		$sel_page = get_page_by_id($_GET['page']);
	} else {
		$sel_subject = NULL;
		$sel_page = NULL;
	}
}



function navigation($sel_subject = null, $sel_page = null) {  
	$subject_set = get_all_subjects();
	// 5. Use returned data
	while ($subject = mysql_fetch_array($subject_set)) { 

	echo "<div class=\"menu-name\">";  echo "<a href=\"edit_subject.php?page=" . urlencode($subject["id"]) .
		"\">{$subject["menu_name"]}</a>";  echo"</div>";
	echo "<div class=\"buying-text\">";  echo "{$subject["content"]}";  echo"</div>";

	echo "<div class=\"image\">"; echo"</div>";

	}
}

?>

 

What I don't understand is, if it's not set how does $sel_page work if they've both got exactly the same code?

Link to comment
Share on other sites

Hi j05hr,

 

That's your problem.  When you call a URL with a ? this is a pararmeter which the $_GET associative array can grab.

 

So, with the http://localhost/estateagent/edit_subject.php?page=15 URL the parameter is 'page' and the value 'page' has is '15'.  The 'page' parameter will be set when the $_GET checks for it in the following code:

 

isset($_GET['page'])

 

For your script to work as you want, the 'subj' parameter needs to be set, and to do this you would use a URL like http://localhost/estateagent/edit_subject.php?subj=15.  However, you will need to look at how your script functions as to whether this is possible.

 

Remember, you can have multiple parameters in a URL using:

 

http://localhost/estateagent/edit_subject.php?page=15&subj=15

 

However, in this example your find_selected_page() function will still fail because both values will be set.  You will need to add something like:

 

if (isset($_GET['subj']) && isset($_GET['page'])) {
//Do something
}

 

But depending on your application and how you're running it this may not be a suitable option.

 

Hope this helps.

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.