[SOLVED] Help with flow control

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
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[SOLVED] Help with flow control

Post by facets »

Hi,

Can anyone help me with flow control?
The if (!$viewstocks) doesn't seem to stop the main code form being viewed in the popup.

tks, Wil

Code: Select all

<?php

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

if (!$viewstocks) {

?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    <? $sql_query = mysql_query("SELECT description, stockId FROM austock");
	echo "<select name=\"stockId\">";
    	while(list($stockId)=mysql_fetch_array($sql_query)) {
		echo "<option value=\"$stockId\">$stockId</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?

if ($_POST['submit']) {  
    
   echo '<pre>';
   print_r($_POST);
   echo "<h1>Submitted to DB</h1><br>"; 
   echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
   echo "</pre>";

// Set Up viewDetails
} elseif ($_POST['stockId']) {
    view($_POST['stockId']); 
 }

if($_GET['action'] == 'view_record' && isset($_GET['stockId'])) view_record();

}

function view($stockId) {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
    }

function view_record() {
    print "hello world <br>";
    $stockId = $_GET['stockId'];
    echo $stockId ;
    print "<br>";
    } 
    
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remember that register_globals are off? $viewstocks won't exist, thus the block it controls will always run. There's no variable for viewstocks to map to either.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

There will be many options for this form once its complete. So would it be better to use the switch statement for this multi-option form?

Is there an example you could show me of either the if or switch way of doing the above?

tks,.. Will
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

so if I add this code

Code: Select all

switch($action) {
   case "view_record":
      print "Hello Print 1";
   break;
   default: 
      print "Hello Print 2";
   break;
}
it still defaults to the 'default' when the action 'view_record' is executed.
what am I doing wrong?

tia, Wil
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

as was said before register_globals are off so you won't have immiedate access to $viewstocks.

do like you do later in your code for $_POST['stockId'] for $viewstocks at the top.
That is if $viewstocks is an option from a previous page.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

that's what is confusing me.
I only need that variable to activate the switch.
I'm unsure on how to stop the form elements appearing on the popup 'results' page.

tia, wil.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

OK, I've rehashed the script adding the switch. but am still getting the same results.
The list summary code is being displayed on the answer (popup) page.

I've run out of ideas. Can anyone help?

Code: Select all

<?php

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

function list_summary() {
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    <? $sql_query = mysql_query("SELECT description, stockId FROM austock");
	echo "<select name=\"stockId\">";
    	while(list($stockId)=mysql_fetch_array($sql_query)) {
		echo "<option value=\"$stockId\">$stockId</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?

if ($_POST['submit']) {  
    
   echo '<pre>';
   print_r($_POST);
   echo "<h1>Submitted to DB</h1><br>"; 
   echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
   echo "</pre>";

// Set Up viewDetails
} elseif ($_POST['stockId']) {
    view($_POST['stockId']); 
 }

if($_GET['action'] == 'view_record' && isset($_GET['stockId'])) view_record();

}

function view($stockId) {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
    }

function view_record() {
    print "hello world <br>";
    $stockId = $_GET['stockId'];
    echo $stockId ;
    print "<br>";
    } 

switch($action) {
   case "view_record":
       view_record();
      print "Hello Print 1";
   break;
   default: 
      list_summary();
       print "Hello Print 2";
   break;
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you aren't pulling $action from anywhere. Use $_GET['action'].. or better yet, properly set up $action.. hint: isset() with a ternary.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

like so? in the function

Code: Select all

function view_record() {
    print "hello world <br>";
    $action = $_GET['action'];
    $stockId = $_GET['stockId'];
    echo $stockId ;
    print "<br>";
    }
or.. anywhere in the script?

Code: Select all

$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it should be at the top of the script so your flow control code gets to see it.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think I may have to give up for tonight. it's getting late.
The following is still displaying incorrectly on the popup.

Code: Select all

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

function list_summary() {
?>
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Thanks to everyone that helped with this issue.
It's now working fine. Well apart from popup window sizes. But that shouldn;t be hard.

Here's the final code for reference..

Code: Select all

<?php

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

// List function - Display Page
function list_summary() {
html_header();
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    <? $sql_query = mysql_query("SELECT description, stockId FROM austock");
	echo "<select name=\"stockId\">";
    	while(list($stockId)=mysql_fetch_array($sql_query)) {
		echo "<option value=\"$stockId\">$stockId</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?

}//Close list_summary function

// Set action Variable
    $action = ($_GET['action']);

// Set Up Submit 
   if ($_POST['submit']) { 
      
      echo '<pre>';
      print_r($_POST);
      echo "<h1>Submitted to DB</h1><br>";
      echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
      echo "</pre>";
   
// Set Up viewDetails
   } elseif ($_POST['stockId']) {
       view($_POST['stockId']);
   }

// Pop up function
function view($stockId) {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
    }

// View Record Function
function view_record() {
    $stockId = $_GET['stockId'];
    echo $stockId;
    print "<br>";
    } 

// Setup switch - View or Submit
switch($action) {
   case "view_record":
       view_record();
   break;
   default: 
      list_summary();
   break;
}

?>
Post Reply