switch <form>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

switch <form>

Post by ol4pr0 »

I am trying to create a page with differant forms using switch();

however i am kinda lost, any help that will get me on the right track would be great, also how to get rid of the Undefined 'cmd' when opening the page.

i would like to create something like this..

Code: Select all

<?
switch($_REQUEST['cmd'])
{
	case 'edit':
	if ($_REQUEST['cmd']=='edit')
	{
		echo 'edit form';
	}
	break;

	case 'delete':
	if ($_REQUEST['cmd']=='delete')
	{
		echo 'Delete form';
	}
	break;

	default:
	echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
	echo '<INPUT TYPE="submit" name="edit" value="edit"><INPUT TYPE="submit" name="delete" value="Delete">';
	echo '</form>';
	break;	
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the "default" case only happens when
  1. no other cases are caught
  2. when a preceeding case falls through. i.e. doesn't "break"
cases only happen when they match the value being switched, the following will do some interesting stuff:

Code: Select all

<?php

$ar = array('J','L','d','adCD','wee');
foreach($ar as $i)
{
switch($i)
{
  case "L":
    echo 'inside "L" '.$i.' '.__LINE__."\n";
  case "J":
    echo 'inside "J" '.$i.' '.__LINE__."\n";
    break;
  case preg_match('#d#',$i):
    echo 'inside "preg_match" '.$i.' '.__LINE__."\n";
    break;
  case 'd':
    echo 'inside "d" '.$i.' '.__LINE__."\n";
    break;
  default:
    echo 'inside default '.$i.' '.__LINE__."\n";
    break;
}
}

?>
Last edited by feyd on Tue Jun 22, 2004 2:02 pm, edited 2 times in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

So what ure saying is that my example should work, when i take out the break after the default ?
Would that also get rid of the undefined 'cmd' error ?


Thanks in advance if that is the case :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for the last entry in a switch, break is often optional.

the undefined cmd error is because you are using $_REQUEST['cmd'] without checking to see if it exists or not...

use [php_man]isset[/php_man]() prior to it to check for it's existance.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Thought so..

I got me this book right here next to me, but it speaks of

<input type="text" name="%s" value="%s">

Now i do not have a clue where the %s are coming from. However i want to create something simular. To be able to edit / delete or whatever, with the information retrieved from a database. Thats why the differant cases.

However i came to the next problem using the switch, is how to get the switch case out of the form ?

meaning how do i determen which case to be used !?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

%s comes from the formatting argument to [php_man]printf[/php_man]() and [php_man]sprintf[/php_man]()
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Thanks i think i got something figures out here.. , still on the 56k so not really time to go to php.net and read about it hehe..

However i faced the following problem.

i am using mysql_fetch_accoc.

Wht i am trying to do is to get some $row['var'] into my my next query.
since i am kinda depending on them for the next query, now i did figure out that that is not going to work...

Is there a way ?

i tried global $row['var'] ( prolly stupid,, what ohwell could give a try ey ? )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like this (untested, example only):

Code: Select all

<?php

$query = mysql_query("SELECT * FROM table_foo WHERE foo_name = 'george' LIMIT 1") or die(mysql_error());
if(!mysql_num_rows($query))
{
  $row = array(); // defaults to use
  $row['foo_name'] = 'george';
  $row['foo_var'] = 1;
}
else
{
  $row = mysql_fetch_assoc($query);
}

$open = '<form name="searchit" action="bif.php" method="post">';
$close = '<input type="submit" value=" Search It! " /></form>';

$middle = '';

switch($row['foo_var'])
{
  case 1:
    $middle .= 'Select one: <select name="peak"><option>joe</option><option>cherrlie</option><option>larry</option></select>';
  case 2:
    $middle .= '<input type="radio" name="food" value="corn" />Corn<br /><input type="radio" name="food" value="peas" />Pears<br /><input type="radio" name="food" value="carrots" />Carrots<br />';
    break;
  case 3:
    $middle .= '<input type="checkbox" name="tele" value="MacGyver" />MacGyver<br /><input type="checkbox" name="tele" value="Jerry Springer" />Jerry Springer<br /><input type="checkbox" name="tele" value="Don Knotts" />Don Knotts<br />';
    break;
  default:
    $middle .= 'You found nothing, bishes.';
}

echo $open.$middle.$close;

?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

|Thanks alot.. i think i can do something with that...

I got something figured out now.. but maby it might be more efficient like that.\
Post Reply