Is it possible to override a page with another include? - php

THE PROBLEM
The problem I've having is I can't override the current page with include.
if ($LS::getUser('clan') || isset($_GET['clan']) && !isset($_GET['search'])) {
include("res/templates/clan-overview.php");
} else {
include("res/templates/clan-search.php");
}
I don't get why I can't change the page with clan-search.php because I said that if $_GET['search'] is NOT set execute the else statement. But for some weird reason I'm still getting true even though I said that if the user has a CLAN || $_GET['clan'] AND $_GET['search'].
What I've Tried
That code I put at the top is actually the modified version of my original code which I thought would fix the problem. If you are interested in looking at the original code which I don't think will help you here it is:
if ($LS::getUser('clan') || isset($_GET['clan'])) {
include("res/templates/clan-overview.php");
} else if (isset($_GET['search'])) {
include("res/templates/clan-search.php");
}

#Drew pierce gave you first response as you need to wrap the OR parts as a group. try this:
if (($LS::getUser('clan') || isset($_GET['clan'])) && !isset($_GET['search'])) {

Related

Drupal JS on content type

Hi I content type 'site_location', /js/site_location.js, and this is on Drupal 7:
I want to run the JS file only on the 'site_location' nodes, but it also runs when I go to the edit page too. Is there any way of not running the javascript when I edit a node of content type 'site_location'?
Here is what I have:
function porto_preprocess_page(&$vars, $hook) {
if($vars['node']->type === "site_location"){
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}
}
Just messing around I found a solution.
arg(2) is 'edit' on the edit page and empty on the view page. Therefore I ended up with:
if($vars['node']->type === "site_location" && arg(2) == ''){
I still wonder though if there are other approaches to this.
Something like this should do the trick:
if ($vars['node']->type === "site_location" && path_is_admin(current_path())) {
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}

Multiple isset checks php

I'm trying to implement multiple if(isset()) statements but I can't get it to work.
example:
if (isset($_GET['a']) or isset($_GET['b'])) {
// HTML
} else {
// HTML
<a href="?link=a>LinkA</a>
<a href="?link=b>LinkB</a>
}
When I click a or b I still got the else statement executed.
I also tried:
if (isset($_GET['a'] or $_GET['b']))
but then I get a error
I'm trying to display different pages on different $_GET requests.
Can someone point me in the right direction or is this not the right way to do this?
Change if (isset($_GET['a']) or isset($_GET['b'])) To:
if ( (isset($_GET['link']) && $_GET['link'] == 'a') OR (isset($_GET['link']) && $_GET['link'] == 'b']) )
You are checking wrong variable.
You need to check $_GET ['link']

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

Hiding a div upon login with php

I am trying to hide a div once a user has logged into there account of my online store (built with cartweaver 4 php) but i just cant seem to get it to work.
the php code for defining wether the user is logged in or not is as followed:
if(!strlen($_SESSION["cwclient"]["cwCustomerID"]) ||
$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
$_SESSION["cwclient"]["cwCustomerID"] === "0" ||
$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
and the div i would like to hide upon login, which contains a table, have been given an id and class of:
<div id="newcustomertable" class='newcustomer'>
I have tried applying this method using css: Show Hide div if, if statement is true
but that ended up giving me an undefined variable error on my testing server.
I admit i am a bit of a php newbie, so if anyone who is able to make more sense of this could help out and possibly find a solution it would be much appreciated.
Thank you.
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
Make sure that the condition check is AFTER the div element getting rendered. Because if you put it before the element the page would not have fully rendered and so the script will not be able to get the div that you want to hide.
REVERSE Condition based on OP's request. I know this is a kluge, folks. Dont flame me!
<div id ="newcustomertable"></div>
<?
if(!isset($_SESSION["cwclient"]["cwCustomerID"]) ||
!strlen(#$_SESSION["cwclient"]["cwCustomerID"]) ||
#$_SESSION["cwclient"]["cwCustomerID"] === 0 ||
#$_SESSION["cwclient"]["cwCustomerType"] == 0 ||
(isset($_SESSION["cwclient"]["cwCustomerCheckout"]) &&
strtolower(#$_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest"))
{
//do nothing
}
else
{
?>
<script>
document.getElementById("newcustomertable").style.display = "none";
</script>
<?
}
?>
You've got at least 3 variables there that could be throwing your undefined variable warning:
$_SESSION["cwclient"]
$_SESSION["cwclient"]["cwCustomerID"]
$_SESSION["cwclient"]["cwCustomerType"]
you need to isset() each of those before doing any tests.
For starters, the strlen function in PHP returns an INT, not a boolean.
Change from:
(!strlen($_SESSION["cwclient"]["cwCustomerID"])
to:
(strlen($_SESSION["cwclient"]["cwCustomerID"]) > 0)
First of all could that not just be simplified to the following if statement to make it easier
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
#code here
}
Second of all to hide the div if they are logged in you could do it like this
if (($_SESSION["cwclient"]["cwCustomerID"]<>NULL)&&($_SESSION["cwclient"]["cwCustomerCheckout"]) == "guest")) {
$shouldhide="style='visibility:hidden;'";
} else {
$shouldhide="";
}
echo"<div id='hidethis' $shouldhide>";
Although if the content is a security risk it'd be better to not output it at all as it's still visible in the source code.

How Can I Move This <td> Tag To The Next Line?

I have a problem:
Here's a block of the code:
function draw()
{
$out_string="";
$out_string.=$this->script;
reset($this->fields);
$num_list_box=0;
while( $field = each($this->fields) )
{
if (isset($this->fields[$field[1]->field]->options))
{
if (preg_match("/<script type=\"text\/javascript\">/i",$this->fields[$field[1]->field]->options[0][1])&& $this->fields[$field[1]->field]->value!="")
{
if ($num_list_box==0) $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
else $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").forValue(\"".$field_prev[1]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
$field_prev[]=$this->fields[$field[1]->field]->value;
$num_list_box++;
} else
{
$field_prev[0]=$this->fields[$field[1]->field]->value;
$num_list_box=0;
}
}
}
$out_string.=$this->draw_title();
$out_string.=$this->draw_header();
$out_string.= "<table class=\"forms\">\n";
$field=array_keys($this->fields);
reset($field);
$ind_first=true;
while( list($pos,$field_name) = each($field) )
{
if ($this->num_cols>0) {
if ($this->fields[$field_name]->col==1){
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_title\">";}
else $out_string.="<td class=\"field_title\">";
$out_string.= $this->fields[$field_name]->title."</td>";
$colspan="";
if ($this->num_cols>1) {
if ($this->fields[$field_name]->col==1 && array_key_exists($pos+1,$field) && $this->fields[$field[$pos+1]]->col==1)
$colspan="colspan=\"3\"";
}
$out_string.="<td class=\"field_value\" $colspan>";
$out_string.=$this->fields[$field_name]->draw()."</td>";
} else
{
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_value\">".$this->fields[$field_name]->title."<br />";
$out_string.=$this->fields[$field_name]->draw()."</td>";
}
}
$out_string.= "</tr></table>\n";
return $out_string;
}
This above block of code produces something like this:
I want it such that in the example provided that the word "Transaction" is above the text box.
Please help, the person that programmed this part is indisposed and we've got a deadline.
Thanks for the help.
P.S. The CSS class name for the text is: field_title and the one for the textbox is field_value
Thanks once more.
You will have to debug that code to find out when the label "Transaction" is getting inserted into that cludge of table code. Once you find where "Transaction" is inserted you can then create new logic to add another TR that colspans the table and place the label on that new row.
Good luck, looks like a headache.
Your code is apparently building a rather complex form based on some data ($fields property of the class). I guess some "field settings" (like col) describe how the form should look like.
So, your code is doing things that are neither described in your question nor in the code itself. Furthermore, we have no clue what the complete form currently looks like, so we can't even guess the intention of the code.
Your request, to let the description appear above the selection box(?) could be done probably throwing away half of the code, but that won't help you.
PS: Please check the FAQ - this site is for questions, not for finding people to do your (or elses) work. You should really have a programmer solve your problem directly having access to the whole page.

Categories