Current location: Hot Scripts Forums » Programming Languages » PHP » Switch()


Switch()

Reply
  #1 (permalink)  
Old 10-19-07, 09:56 AM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
Switch()

Hey,

Just wondering. Can while() be used in the switch() to create case's from the loop?

such as:

PHP Code:


// This is not real coding. Just a demo

$result query();

while(
$row fetch($result)){
       
       case 
$row['name'];
       
              
// content here.

       
break;

}

// ???????????????????????? 
Thanks.
Reply With Quote
  #2 (permalink)  
Old 10-19-07, 09:59 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
No, you cannot create cases like this, dynamically.

If you tell us what you're trying to achieve, we may can help you find another solution.
Reply With Quote
  #3 (permalink)  
Old 10-19-07, 10:52 AM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
Hey,

I have another solution but I was just wondering. I tested this and your right it doesn't work. I am going to use if statements

Thanks.
Reply With Quote
  #4 (permalink)  
Old 10-20-07, 06:53 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
blin_shade, I like the way you think. I believe there is a way to create
a SWITCH() case... case... case... using a database. I believe this method
would give you a lot of flexibility.

PHP has a function called eval(). It evaluates all data as PHP code and
executes it. So, all you would need is a table with two fields. A case field
and an action field. The case field can hold any case. And the action field
can hold any PHP code.

Something like this:
PHP Code:

<?php
$case 
= isset($_POST["case"]) ? $_POST["case"] : isset($_GET["case"]) ? $_GET["case"] : 1;
$server   ""// MySQL hostname
$username ""// MySQL username
$password ""// MySQL password
$dbname   ""// MySQL db name
$table ""// Table name
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$results mysql_query("SELECT * from $table");
while(
$row mysql_fetch_array($results)){if($case == $row["case"]){eval($row["action"]);}}
?>
__________________
Jerry Broughton

Last edited by job0107; 10-20-07 at 07:16 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
switch domain depends on IP address callmewhateveruwant PHP 5 04-07-07 09:34 AM
switch ($page)..? callmewhateveruwant PHP 6 11-01-06 03:50 PM
Using a integer in switch() perleo PHP 5 01-06-06 02:27 PM
using switch...case stmt w/ array or for loop?, switch..case, arrays, for loops ski_woman PHP 2 10-20-04 07:51 AM
Malfunctioning switch statement and processing cstallins PHP 2 10-20-03 08:48 PM


All times are GMT -5. The time now is 07:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.