Jump to content

How do i make this work? if else array


Recommended Posts

how do i make this work? I need the if else statement to work.. like if i select ''samsung'' and i press submit the file must be included... but i just dont get the right code.. things that i've tried:

if ($device="samsung")if (in_array("samsung" , $devices))if (in_array($devices="samsung"))and some others that i can't remember

 <html><body> <?php$devices = array('iphone' => 'iPhone',				 'samsung' => 'Samsung',				 'nokia' => 'Nokia',				 'sonyericsson' => 'Sonyericsson',				 'htc' => 'HTC',);echo '<form method="post">';  echo '<select>';	foreach($devices AS $key => $value)			  echo '<option value="'. $key .'">'. $value .'</option>';  echo '</select>';  echo '<input type="submit" value="Submit">';echo '</form>'; if (in_array($devices , 'samsung')){  include 'C:\xampp\htdocs\Websites\myfolder\includes\samsungmod.php';}					  ?></body></html>

PLEASE HELP ME! thanks

Edited by RmonDr
Link to post
Share on other sites

to check if a key exists in the array:

if (isset($devices['samsung'])){}

or to check if a value exists in the array:

if (in_array('Samsung', $devices)){}

also for the include path, you need to use 2 backslashes for each one. C:\\xampp\\.. (doesn't apply to forward slashes)

Link to post
Share on other sites
to check if a key exists in the array:
if (isset($devices['samsung'])){}

or to check if a value exists in the array:

if (in_array('Samsung', $devices)){}

also for the include path, you need to use 2 backslashes for each one. C:\\xampp\\.. (doesn't apply to forward slashes)

but it now shows the include regarding of the submit.. i need it to hide when it hasnt been selected.. like if i select ''iphone'' it will show an include with iphone models, and when i select samsung, it will show an include with samsung models, so when i select item and ''submit'' that it only then shows the include, and when i have not yet clicked submit that it stays blank
Link to post
Share on other sites

he was just giving you examples of how to check. You still need to apply the logic to your given situation. You need to give your select tag and submit button name attributes (with values). Also, you should really consider using relative path for your includes, because If/when you move this script to a server, your paths won't work. For your immediate question, consider something like this:

 <html><body> <?php$devices = array(  'iphone' => array(	'label' => 'iPhone',	'includePath' => 'path/to/iphone-include.php'  ),  'samsung' => array(	'label' => 'Samsung',	'includePath' => 'path/to/samsung-include.php'  ),  'nokia' => array(	'label' => 'Nokia',	'includePath' => 'path/to/nokia-include.php'  ),  'sonyericsson' => array(	'label' => 'Sonyericsson',	'includePath' => 'path/to/sonyericsson-include.php'  ),  'htc' => array(	'label' => 'HTC',	'includePath' => 'path/to/htc-include.php'  )); echo '<form method="post">';echo '<select name="devices">';foreach($devices as $key => $value){  echo '<option value="'. $key .'">'. $value['label'] .'</option>';};echo '</select>';echo '<input type="submit" value="Submit" name="submit">';echo '</form>'; //if form was submitted, includeif(isset($_POST['submit'])){  $selectedDevice = $_POST['devices'];  include $devices[$selectedDevice]['includePath'];}					?></body></html>

Edited by thescientist
Link to post
Share on other sites
he was just giving you examples of how to check. You still need to apply the logic to your given situation. You need to give your select tag and submit button name attributes (with values). Also, you should really consider using relative path for your includes, because If/when you move this script to a server, your paths won't work. For your immediate question, consider something like this:
 <html><body> <?php$devices = array(  'iphone' => array(	'label' => 'iPhone',	'includePath' => 'path/to/iphone-include.php'  ),  'samsung' => array(	'label' => 'Samsung',	'includePath' => 'path/to/samsung-include.php'  ),  'nokia' => array(	'label' => 'Nokia',	'includePath' => 'path/to/nokia-include.php'  ),  'sonyericsson' => array(	'label' => 'Sonyericsson',	'includePath' => 'path/to/sonyericsson-include.php'  ),  'htc' => array(	'label' => 'HTC',	'includePath' => 'path/to/htc-include.php'  )); echo '<form method="post">';echo '<select name="devices">';foreach($devices as $key => $value){  echo '<option value="'. $key .'">'. $value['label'] .'</option>';};echo '</select>';echo '<input type="submit" value="Submit" name="submit">';echo '</form>'; //if form was submitted, includeif(isset($_POST['submit'])){  $selectedDevice = $_POST['devices'];  include $devices[$selectedDevice]['includePath'];}					?></body></html>

Thanks! pfff... another programming mistery solved
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...