Me again :P. another problem

T

TheJkWhoSaysNi

Guest
i have a problem with or ("||"), heres what i have
Code:
if($page == 'Ranks' || 'Rules' || 'About' || 'History' || 'Links'){
    $title = str_replace("_", " - ", "$page");
    $title = "$page";
    include ("header.php");
    content("<$page>");
    include("footer.php");
}

else if($page == 'League_Join' || 'League_Staff' || 'League_Rules' || 'League_Players' || 'League_Standings' || 'League_Report' || 'League_Games' || 'League_Statistics' || 'League_Stats1' || 'League_Stats2' || 'League_Stats3' || 'League_Stats4' || 'League_Stats5' || 'League_Stats6' || 'League_Stats7' || 'League_Profile'){
       $title = str_replace("_", " - ", "$page");
       $leaguePage = str_replace("_", "/", "$page");
       echo("page: $page,&username");
       include("header.php");
       member();
       include("$leaguePage" + '.php');
       include("footer.php");
}

The things in the first IF are working properly, but the pages in the in, "else if" are acting like the ones in the if :(

Can anyone explaing what i've done wrong?
Also, my str_replace isnt working. Why?

Thanks for any help.
 
J

Jonty

Guest
Hi mate, sorry, in a real rush so I don't have time to check your code as I should, but I believe 'else if' is Javascript's etc. favoured syntax. PHP uses 'elseif', or so I seem to recall. Just deleted the space between the 'else' and the 'if' and it should work. Got to dash!
 
T

TheJkWhoSaysNi

Guest
actually, the space is only there because elseif diddnt work :(
 
T

TheJkWhoSaysNi

Guest
nevermind i got it!
needed to change
if($page == 'Ranks' || 'Rules' || 'About' || 'History' || 'Links'){


into
if($page == 'Ranks' || $page == 'Rules' || $page == 'About' || $page == 'History' || $page == 'Links'){
 
S

Shocko

Guest
That's something i've noticed before(in perl as it happens, but same thing). I wondered whether you could do:
if($page == ('Ranks' || 'Rules' || 'About' || 'History' || 'Links'))

However i'm not totally sure how that would evaluate... I can't remember whether i tried that and it didn't work, or i just did it the long way(how you've solved it). As for the elseif thing, well it doesn't matter whether you use that or else if... With the latter you're using basic a if and else condition, but stringing together several of them; If you go through how that would execute, it's exactly the same as the elseif condition, which works exactly the same thing, and i gather is there for simplicity.
 
J

Jonty

Guest
A wealth of knowledge as ever, Shocko, ty.

I was thinking before of something I use from time to time in Javascript which in theory should be easily 'portable' to PHP. Basically, to avoid things like . . .

Code:
if($page == 'Ranks' || $page == 'Rules' || $page == 'About' || $page == 'History' || $page == 'Links') {

. . . which work but aren't as perhaps as efficient as we'd like, especially with many many different 'or' values, I use the following trick . . .

Code:
[b]1. Create your array with your required 'or' values[/b]
$MyOrValueArray = ["Ranks", "Rules", "About", "History", "Links"];

[b]2. Create a simple 'for' loop[/b]
for (var i=0; i<MyOrValueArray.length; i++)
{
  if ($page == MyOrValueArray[i])
  {
    [i]. . . Execute some code specific to the first array . . .[/i]
  }
  else
  {
    [i]. . . Execute some code not specific to the first array . . .[/i]
  }
}

Now sure, that code is shockingly basic and would require modification to get it into a stable state (and to make it conform to PHPs syntax etc), but I personally find that sometimes (not always) this can be a fairly efficient way of coding such things. It comes into it's own when you have a lot of different 'or' values you need to check against.
 
T

TheJkWhoSaysNi

Guest
Hmm, i might try that.

At the moment, i have seveal categorys with several pages in each. Different 'Types' of pages. As seen in the first example.

Currently I am creating the sidebar manually. Would it be possible to Store the Pages and the types, and the cateogorys in the index page, then have the sidebar read the page names, and create the various links, categorys etc?
 
T

TheJkWhoSaysNi

Guest
ok, i'm trying what i suggested above, but using mysql because its much easier.

this is what i have so far:

Code:
<?
$databaseserver = "localhost";
$databaseuser = "ud";
$databasepass = "*****";
$databasename = "ud";

$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM Pages WHERE Name = '$page'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);


$type = $row["Type"];

function Type1() {
    global $page;
    $title = str_replace("_", " - ", "$page");
    include ("header.php");
  //  content("<$page>"); remove this, the content function isnt set up yet...
    include("footer.php");
    echo("maybe it cant find header or footer, i'll echo this too, just to make sure its the if thats wrong...");
  }

$functiontype = "Type$type";


echo("Type: $type  Page: $page  Num_rows: $num");

if ($type == '1'){

$functiontype();

}


?>
you can check this page here: http://www.ud.barrysworld.net/indextest.php?page=Ranks

(Only ranks will work, because its the only page i added to the database, so far)

the query is fine. its that if. (Why is it always the really simple stuff that i get stuck on? :rolleyes: ) The if ($type == '1') does not work!! $type does equal 1!!! as you can see by going to the page.

Also, i tried removing the function and putting the includes directly into the if. That diddnt work either :(

Anyone have any idea what is wrong?
 
S

Shocko

Guest
One thing to note with PHP and other languages decended from C, is that you don't need to put numbers inside quotation marks. I doubt this would be your problem, because afaik PHP is very tolerant when it comes to syntax, however it's just something i've noticed makes code look sharper.

Have you used a pointer to a function there? I've generally ignored pointers with PHP, and i've sort of stopped learning C(for the time being)... So i've not really come accross practical uses for pointers to functions... *takes note*

I really can't see where your problem would lie, but good luck. Jonty, that is quite a nice way of doing that condition, especially for a webpage where specifying available pages is best done at the top, for tidyness and maintanance. Devil, feel free to ask as many questions as you want, because i'm learning stuff from these threads as well :)
 
T

TheJkWhoSaysNi

Guest
okay. i changed it to this:
(Removed the if. becasuse i dont actually need it, the query does it all instead)

Code:
$databaseserver = "localhost";
$databaseuser = "ud";
$databasepass = "*****";
$databasename = "ud";

$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM Pages WHERE Name = '$page'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);


$type = $row["Type"];

function Type1() {
    global $page,$type;
    $title = str_replace("_", " - ", "$page");
    include ("header.php");
  //  content("<$page>"); remove this, the content function isnt set up yet...
    include("footer.php");
    echo("maybe it cant find header or footer, i'll echo this too, just to make sure its the if thats wrong...");
  }

$functiontype = "Type$type";


echo("Type: $type  Page: $page  Num_rows: $num ");
echo("Funtiontype: $functiontype");


$functiontype();
echo("TEST");

I dont see "TEST" on the page. meaning everything after the $functiontype(); is not working. The same thing happens if i use Type1(); where functiontype is :(

using this:

Code:
$databaseserver = "localhost";
$databaseuser = "ud";
$databasepass = "*****";
$databasename = "ud";

$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM Pages WHERE Name = '$page'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);


$type = $row["Type"];

$functiontype = "Type$type";


echo("Type: $type  Page: $page  Num_rows: $num ");
echo("Funtiontype: $functiontype");

    $title = str_replace("_", " - ", "$page");
    include ("header.php");
  //  content("<$page>"); remove this, the content function isnt set up yet...
    include("footer.php");
    echo("maybe it cant find header or footer, i'll echo this too, just to make sure its the if thats wrong...");


echo("TEST");

i have exactly the same problem... this is getting strange.
http://www.ud.barrysworld.net/indextest2.php?page=Ranks
 

Users who are viewing this thread

Top Bottom