Jump to content

Help Improve my Code - PHP (I'm a beginner)


Junaid R

Recommended Posts

<?php
$game = $_GET["id"];
if ($game == ballpool)
{
<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>;
}
elseif ($game == minisoccer)
{
<div id="miniclip-game-embed" data-game-name="soccer-stars" data-theme="0" data-width="750" data-height="450" data-language="en"></div>
}
else
{
echo "Invalid Request";
}
?>
Link to comment
Share on other sites

well.. yes. i have gaminig website. and i've made just one page for playing online for all games. i have used this php syntax in my page to get variable from the url and compare it with the if statements and execute the true code block. this code works fine when i replace the

 

<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>;
thing with
echo "Whatever";
it works fine then..
Link to comment
Share on other sites

As alluded to, what kind of help are you looking for? As general observations, I will say that

  1. The if statement is incorrect, ballpool and minisoccer should be in quotes
  2. Your markup also needs to be in quotes
  3. You are repeating markup generation that you only need to do once. Perhaps something like this (which is easily extendable)

<?php$game = $_GET["id"]; //make an array to store all specific properties of each game$gamesConfig = array(  "ballpool" => array(    "name" => "8-ball-pool-multiplayer"    "height" => 520  ),  "minisoccer" => array(    "name" => "soccer-stars"    "height" => 450  )); $output = ''; //test that the game provided existsif ($gamesConfig[$game]) {  //get the config for the game  $config = $gamesConfig[$game];   //build up the HTML using values from the config  $output .= '<div id="miniclip-game-embed" data-game-name="' . $config["name"] . '" ';  $output .= 'data-theme="0" data-width="750" data-height="' . $config["height"] . '" ';  $output .= 'data-language="en"></div>';} else {  $output = "Invalid Request";} echo $output; ?>
  • Like 1
Link to comment
Share on other sites

You can do it any way you want to do it.

this code works fine when i replace the<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>;thing withecho "Whatever";it works fine then..

Look at the difference. One of them is missing an echo statement and quotes.
Link to comment
Share on other sites

man just suggest me what to read to learn some advanced php.. all the tutorials are too old and simple. and bro you were saying that echo and quotes are missing. you can also see that the statement which is without echo and qoutes does conatain a lot of quotes itself.. so they conflict with eachother

Link to comment
Share on other sites

you can also see that the statement which is without echo and qoutes does conatain a lot of quotes itself.. so they conflict with eachother

I know that, there are several ways you could solve that issue. Just leaving out the echo statement and the quotes is not one of the ways to solve it.
if ($game == 'ballpool'){  echo '<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>';}
if ($game == 'ballpool'){  echo "<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>";}
if ($game == 'ballpool'){?><div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div><?php}
if ($game == 'ballpool'){  echo <<<END<div id="miniclip-game-embed" data-game-name="8-ball-pool-multiplayer" data-theme="0" data-width="750" data-height="520" data-language="en"></div>END;}
http://php.net/manual/en/language.types.string.phpThere are plenty of books here if you want to learn:http://shop.oreilly.com/category/browse-subjects/programming/php.do?sortby=bestSellers&sortType=1
Link to comment
Share on other sites

you can also see that the statement which is without echo and qoutes does conatain a lot of quotes itself.. so they conflict with eachother

just because you have quotes _in_ it, does not mean it was syntactically correct. The entire string you were trying to generate needed be enclosed in quotes, just like the example you showed that worked.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...