Page 1 of 1
switch <form>
Posted: Tue Jun 22, 2004 1:50 pm
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;
}
Posted: Tue Jun 22, 2004 1:55 pm
by feyd
the "default" case only happens when
- no other cases are caught
- 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;
}
}
?>
Posted: Tue Jun 22, 2004 1:56 pm
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

Posted: Tue Jun 22, 2004 2:04 pm
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.
Posted: Tue Jun 22, 2004 2:14 pm
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 !?
Posted: Tue Jun 22, 2004 2:49 pm
by feyd
%s comes from the formatting argument to [php_man]printf[/php_man]() and [php_man]sprintf[/php_man]()
Posted: Tue Jun 22, 2004 7:56 pm
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 ? )
Posted: Tue Jun 22, 2004 8:45 pm
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;
?>
Posted: Wed Jun 23, 2004 2:12 am
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.\