Page 1 of 1
conditional statments & array
Posted: Mon Nov 08, 2010 2:26 pm
by jose07
Hey guys here is where i'm stuck:
i have adverts on the site but i have set permission on the adverts to be either 'public' or 'restricted' so based on that i want to create a statement with three possible outcomes, it should check the values of the columns and if the is an advert it will check the permission if the permission 0 (public) it shows the ad if permission 1 (restricted) it will direct the user to a log-in page however if the user is logged on then it will just show all the adverts.
These is the code i'm using but it is only executing the else line (so when the user clicks on the ad it takes them straight to the login page even if the ad is set to '0')....i checked the error log and i get the msg :PHP Notice: Undefined variable: SiteDir in
code:
Code: Select all
<?php
$qry_getAdverts = mysql_query("SELECT advertID,accesslevelID FROM adverts_adverts");
$row_getAdvert = mysql_fetch_assoc($qry_getAdverts);
if($qry_getAdverts["AdvertID"] > 0 && ($qry_getAdverts["accesslevelID"] == 0 )) {
echo($row_getAdvert["Content"]);
}
else {
echo '<script type="text/javascript
">window.location.href=\'' . $SiteURL . '/my-account/login.php\';</script>' ;
}
?>
I tried put it as an array and use a while loop but still it didn't work
Any tips much appreciated!
Thanks
Re: conditional statments & array
Posted: Mon Nov 08, 2010 3:41 pm
by DigitalMind
i didn't read your post. first replace
Code: Select all
if($qry_getAdverts["AdvertID"] > 0 && ($qry_getAdverts["accesslevelID"] == 0 )) {
with
Code: Select all
if($row_getAdverts["advertID"] > 0 && ($row_getAdverts["accesslevelID"] == 0 )) {
Re: conditional statments & array
Posted: Mon Nov 08, 2010 3:50 pm
by flying_circus
Code: Select all
<?php
# Get all records from the advert table
$qry_getAdverts = mysql_query("SELECT `advertID`, `accesslevelID` FROM `adverts_adverts`;");
# Select a single record
$row_getAdvert = mysql_fetch_assoc($qry_getAdverts);
# I assume this line is a typo? $qry_getAdverts is a resultset, you need to pull records from this.
//if($qry_getAdverts["AdvertID"] > 0 && ($qry_getAdverts["accesslevelID"] == 0 )) {
# This is kind of silly. You should be putting this type of stuff into your SQL query.
// Ex 1: "SELECT `advertID`, `accesslevelID` FROM `adverts_adverts` WHERE `advertID` > 0;"
// Ex 2: "SELECT `advertID`, `accesslevelID` FROM `adverts_adverts` WHERE `advertID` > 0 AND `accesslevelID` = 0;"
if($row_getAdvert["AdvertID"] > 0 && ($row_getAdvert["accesslevelID"] == 0 )) {
# How can we echo something that we haven't queried?
// Modify Query to: "SELECT `advertID`, `accesslevelID`, `Content` FROM `adverts_adverts`;"
echo($row_getAdvert["Content"]);
} else {
# This is also very silly. What if the user has disabled javascript on their browser? Lets just send a HTTP header instead.
//echo '<script type="text/javascript">window.location.href=\'' . $SiteURL . '/my-account/login.php\';</script>';
header("location: {$SiteURL}/my-account/login.php");
exit();
}
?>
Re: conditional statments & array
Posted: Tue Nov 09, 2010 1:06 pm
by jose07
Thanks for the reply and thanks for correcting my mistake of using action script to re-direct the user. I have another question
I can't user headers because the script already uses other set headers which then conflict if i add another one on the if statement. my question is how can i direct the user to a particular page 'login' from the if statement without using javascript or headers? and if i have to use header how do i set it up so it doesn't conflict with the others already set?
Code: Select all
else {
//echo '<script type="text/javascript">window.location.href=\'' . $SiteURL . '/my-account/login.php\';</script>';
header("location: {$SiteURL}/my-account/login.php");
exit();
}
Re: conditional statments & array
Posted: Tue Nov 09, 2010 1:10 pm
by flying_circus
Which other headers have you sent that are conflicting? Are you receiving an error?
Re: conditional statments & array
Posted: Tue Nov 09, 2010 7:09 pm
by jose07
Yes i get this: PHP Warning: Cannot modify header information - headers already sent by (output started at /.../.../.../.../inc_header.php:6) in /..../..../..../..../adverts.php on line 64
And it won't take me to the login page instead it just shows half the advert and no footer which makes me suspect it is stopping the code there.
Re: conditional statments & array
Posted: Tue Nov 09, 2010 11:40 pm
by flying_circus
jose07 wrote:Yes i get this: PHP Warning: Cannot modify header information - headers already sent by (output started at /.../.../.../.../inc_header.php:6) in /..../..../..../..../adverts.php on line 64
And it won't take me to the login page instead it just shows half the advert and no footer which makes me suspect it is stopping the code there.
Yup, as I suspected.
You must make all header calls before sending any output. Output can be anything from part of your HTML code to a blank space or extra line in your php documents. You must be conscious of this when writing your code. A bit of a crutch that can make life easier is to use output buffering. One of the first lines of code in your project could be: "ob_start();" This will buffer all output and allow you to send headers even if you've accidentally spit out output before sending the header.
This is actually a VERY common problem with newer PHP developers. Search this forum and you will find an abundance of information on the dreaded "header()" call.
Re: conditional statments & array
Posted: Wed Nov 10, 2010 8:19 am
by jose07
Thanks for that, you're brilliant! compared to all the other forums your replies were the only ones that actually helped me solve the problem, so thanks for taking time to explain. The ob_start(); worked beautifully. Thanks again and keep up the good work!