nofel
November 13, 2011, 4:53pm
1
hi all,
i working on a sandbox for CMS and i am stuck with a problem. i had nothing being displayed on page so i had to var_dump the variable and it showed me its null, whereas it has values in database against the id. where can i be going wrong? here is my code
function get_pages_by_id($page_id){
$query = "SELECT * FROM pages WHERE id = '$page_id'";
$result = mysql_query($query);
confirm($result);
if($page_set = mysql_fetch_array($result)){
return $page_set;
}
}
<?php
if(isset($_GET['subj'])){
$sel_sub = $_GET['subj'];
$sel_page = "";
}
elseif(isset($_GET['page'])){
$sel_sub = "";
$sel_page = $_GET['page'];
}else{
$sel_sub = "";
$sel_page = "";
}
$sub = get_subjects_by_id($sel_sub);
$page = get_pages_by_id($sel_page);
?>
the second bit of code, get subjects in subjects are clicked. which is working fine. but the page function (first php code) passes the value in url of id, but don’t echo anything. so i took var dump and it says null
Do an echo of $query in the function and see what it says.
And what happens if this ‘if’ in the functions is not true?
if($page_set = mysql_fetch_array($result)){
What will the function return?
nofel
November 13, 2011, 6:03pm
3
guido2004:
Do an echo of $query in the function and see what it says.
SELECT * FROM pages WHERE id = '2'
And what happens if this ‘if’ in the functions is not true?
if($page_set = mysql_fetch_array($result)){
What will the function return?
i did print_r as echo didn’t work, so here is the result
Array ( [0] => 2 [id] => 2 [1] => 1 [subject_id] => 1 [2] => Our Mission [menu] => Our Mission [3] => 2 [position] => 2 [4] => 1 [visible] => 1 [5] => Our mission statement is [content] => Our mission statement is )
i wonder if till here, the query and result are coming all correct. wht happens that when i pass argument in function, it return null
Cups
November 13, 2011, 6:15pm
4
Don’t make assumptions about what key data is being passed to the function without checking them too.
var_dump($sel_sub);
var_dump($sel_page);
nofel
November 13, 2011, 6:23pm
5
Selected Subject string '' (length=0) Selected Page string '1' (length=1)
so the values swap, when i click subject it give this
Selected Subject string '1' (length=1) Selected Page string '' (length=0)
nofel
November 13, 2011, 6:55pm
6
i went ahead and took a var dump of
$sub = get_subjects_by_id($sel_sub);
$page = get_pages_by_id($sel_page);
and as expected the value of $sub returned value, while $pages returned with Boolean false.what am i doing wrong?
nofel
November 13, 2011, 8:03pm
7
found the problem. but i don’t know why its doing it, this is what i echo
<?php echo $sub['menu_name']; ?>
<?php echo $page['menu'];?>
it doesn’t echo the page name there, but if i put it somewhere lets say
<?php
$sub = get_subjects_by_id($sel_sub);
$page = get_pages_by_id($sel_page);
?>
<div><?php echo $page['menu'];?></div>
it work. what can be the reason of this stupid bug?
nofel:
found the problem. but i don’t know why its doing it, this is what i echo
<?php echo $sub['menu_name']; ?>
<?php echo $page['menu'];?>
it doesn’t echo the page name there,
There where? You only posted two lines of code.
nofel
November 14, 2011, 9:25am
9
if i call the function on top of it the echo and not in start of the page.like so,
<h2><?php echo $sub['menu_name']; ?>
<?php
$page = get_pages_by_id($sel_page);
echo $page['menu'];?>
</h2>
<div><?php
echo $page['content'];
//print_r($pg);
?></div>
nofel
November 14, 2011, 9:30am
11
hahah, it does. but that is one strange thing. that it doesn’t work when function is called in start of page n work here,anyhow it works in the end