Jump to content

Archived

This topic is now archived and is closed to further replies.

ItsWesYo

Is this correct?

Recommended Posts

[b]links.php[/b] (links will be displayed from a mysql database)
[code]
<?php
include ("http://www.evermoreforums.com/wes/header.php");
$page = ((isset($_REQUEST['id']))?($_REQUEST['id']):(''));

switch($page)
{


case "1": {
echo ( "

testing

" );
break; }



default: {
echo ( "

blah, the main page

" );
break; }


}
?>
[/code]


[b]db.php[/b]
[code]
<?
mysql_connect("localhost","user","password");
mysql_select_db("my_database");
[/code]


[b]The question is:[/b]
Would I do this to 'links.php'?

[code]
<?php
include ("http://www.evermoreforums.com/wes/header.php");
$page = ((isset($_REQUEST['id']))?($_REQUEST['id']):(''));

switch($page)
{


case "1": {
include ("db.php");
$result = mysql_query("select * from news");
while($r=mysql_fetch_array($result))
{
  $title=$r["title"];
  $message=$r["message"];

  echo "$title <br> $message<br>";
}
?>
" );
break; }


default: {
echo ( "

blah, the main page

" );
break; }


}
?>
[/code]




Sorry if it seems confusing

Share this post


Link to post
Share on other sites
ok the way that I use a switch statement is like this
[code=php:0]
<?php
function getpage($page) {
     switch ($page) {
          case "whatever":
// put your php stuff here
          break;
          case "whatever2":
//dido
          break;
     default:
//put your default page at the end. There is no need for another break after this
    }
}
getpage($_GET['page']);//you need this to execute your code
?>[/code]

Now you can make a link to it like this [b]yourfile.php?page=whatever[/b]

Hope this helps,
Tom

Share this post


Link to post
Share on other sites
Well, too bad that code doesn't work :P

"Parse error: parse error, unexpected T_VARIABLE, expecting T_STRING in /home/evermore/public_html/wes/beta/index.php on line 2"

Share this post


Link to post
Share on other sites
sorry man. Made a small error.
change this

[code=php:0]function $getpage($page) {[/code]

to

[code=php:0]function getpage($page) {[/code]


Good Luck,
TOm


Share this post


Link to post
Share on other sites
also you should only have to include your database connection file once. Do this before the start of the function.

Share this post


Link to post
Share on other sites
everytime i remove "// put your php stuff here" and the other two, it comes up with an error on a break code.

Share this post


Link to post
Share on other sites
All I did was replace the // with my own words. Knowing me, I probably did it wrong >_>

<?php
function getpage($page) {
    switch ($page) {
          case "whatever":
fdfd
          break;
          case "whatever2":
dfdfdfd
          break;
    default:
//put your default page at the end. There is no need for another break after this
    }
}
getpage($_GET['page']);//you need this to execute your code
?>

Share this post


Link to post
Share on other sites
You will want to omit any [code=php:0]<?php[/code] or [code=php:0]?>[/code] in the cases. I will insert your code above into my code so that you can see a working example.

[code=php:0]
<?php
include("db.php");
function getpage($page) {
    switch ($page) {
         case "1":
$result = mysql_query("select * from news");
while($r=mysql_fetch_assoc($result)) {
    $title=$r['title'];
    $message=$r['message'];
    echo "$title <br> $message<br>";
}
mysql_free_result($result);
         break;
         case "whatever2":
//dido
         break;
    default:
//put your default page at the end. There is no need for another break after this
   }
}
getpage($_GET['page']);//you need this to execute your code
?>
[/code]

And yes in php a coment is ether started with // or /* and ends with */

Share this post


Link to post
Share on other sites

×

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.