Jump to content

Loop Problem?


snowdog

Recommended Posts

Here is the input code

<form action="process_add_staff.php" method="post">

<div class="form_default">
<fieldset>
<legend>Add Staff</legend>
 <p>
<label for="occupation">Choose Dealer</label>
<select name="dealer_id">
<option>Choose Dealer</option>

 <?php
 // Load The dealers in for the franchisee


 include("include/db_connect.php");


 //SESSION_START();

 $franchise_id = $_SESSION['franchise_id'];

 // Query the database and get dealers


 $SQL = "SELECT * FROM dealers WHERE franchise_id = '$franchise_id' ORDER BY name ASC";
 $result = mysql_query($SQL) or die(mysql_error());	

 while($run = mysql_fetch_array($result))
 {
 ?><option value="<?php echo $run['id']; ?>"><?php echo $run["name"]; ?></option>	

 <?php

 }


 ?>
</select>
 </p>
<p>
<label for="name">First Name</label>
<input type="text" name="first_name" class="sf" />
</p>


<p>
<label for="name">Last Name</label>
<input type="text" name="last_name" class="sf" />
</p>
 <p>
<label for="department">Department</label>
<select name="department">
<option>Choose Department</option>

 <?php
 // Load The departments in
 // Query the database and get dealers


 $SQL = "SELECT * FROM departments WHERE franchise_id = '$franchise_id' ORDER BY department ASC";
 $result = mysql_query($SQL) or die(mysql_error());	

 while($run = mysql_fetch_array($result))
 {
 ?><option value="<?php echo $run['id']; ?>"><?php echo $run["department"]; ?></option>		
 <?php	
 }


 ?>
</select>

 </p>
 <p class="number">
<label for="name">Phone Number</label>
<input type="text" name="phone[]" class="sf" />
<select name="type[]">
<option value="1">Office</option>
<option value="2">Cell</option>
<option Value="3">Fax</option>
</select>
 <a href="javascript:void();" class="add_phone">Add More</a>


 </p>
<p>
<label for="name">Phone Number</label>
<input type="text" name="phone_1" class="sf" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" class="sf" />
</p>
<p>
<button>Submit</button>
</p>

</fieldset>
</div><!--form-->


</form>

 

and here is the process page. I have been trying to isolate the error but I just get a blank screen. I am trying to echo out the multiple phone numbers and types but nodda.

 

<?php


include("include/db_connect.php");

SESSION_START();




//$dealer_name = $_REQUEST['d_name'];
//$d_address = $_REQUEST['d_address'];
//$d_city = $_REQUEST['d_city'];
//$d_country = $_REQUEST['d_country'];
//$d_state_prov = $_REQUEST['d_state_prov'];
//$d_zip = ereg_replace("[^0-9a-zA-Z]","", $_REQUEST['d_zip']);
//$d_phone_1 = ereg_replace("[^0-9]", "", $_REQUEST['d_phone_1']);
//$d_email = $_REQUEST['d_email'];
//$franchise_id = $_SESSION['franchise_id'];
//$username = $d_email;
//$password = $d_phone_1;
//$website = $_REQUEST['website'];






//$SQL = "INSERT INTO dealers (franchise_id, name, address, city, state, zip, country, website, created, modified)
VALUES ('$franchise_id','$dealer_name','$d_address','$d_city','$d_state_prov','$d_zip','$d_country','$website',NOW(),NOW())";


//$result = mysql_query($SQL) or die('Query failed: ' . mysql_error());



//$dealer_id = mysql_insert_id();


//$SQL = "INSERT INTO login (franchise_id,dealer_id,department_id,first,last,email,username,password,created,modified)
VALUES ('$franchise_id','$dealer_id','1','$first_name','$last_name','$d_email','$username','$password',NOW(),NOW())";
//$result = mysql_query($SQL) or die('Query failed: ' . mysql_error());




// Redirect back to add_dealer.php


//header('Location: add_staff.php?m=1') ;



for( $i = 0; $i < count($_REQUEST['phone']); $i++ )
{
echo(" $_REQUEST['type'][$i] = $_REQUEST['phone'][$i] <br >");
}



?>

 

Any help would be great. Thanks

 

Snowdog

Edited by snowdog
Link to comment
Share on other sites

If you take a look at the color highlighting that the forum software did to your posted code (and you should be using a programming editor that does color highlighting too), you will notice that your sql query statements that are made up of more than one line are showing the second line as php code. You commented out the first line in a couple of your $sql statements, but the second line is now being treated as a line of php code by itself and is producing a php syntax error, which is a fatal parse error and your code never runs.

 

You need to comment out all the lines of a multiple-line query statement.

 

You need to have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini on your development system so that php will report and display all the errors it detects. If php is running as a cgi application and supports a local php.ini, you can put the settings into a local php.ini (you may have to repeat this in each folder where you make http requests to a file at, depending on how the server is setup to read local php.ini files.). If php is running as an Apache module, you can put equivalent settings (the syntax and values are different) into a .htaccess file. Because the php syntax error is in your main file, putting the two settings into your main code won't turn on the settings because your main code never runs.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

This isn't a server issue, unless you had a problem setting the error_reporting/display_errors settings on your server.

 

It's a problem in your code.

 

You should be learning php and developing/debugging php code on a local development system. You will save a TON of time. Only complete and tested code should be put onto a live server.

Link to comment
Share on other sites

Here is the code

 

<?php


// include("include/db_connect.php");

// SESSION_START();




//$dealer_name = $_REQUEST['d_name'];
//$d_address = $_REQUEST['d_address'];
//$d_city = $_REQUEST['d_city'];
//$d_country = $_REQUEST['d_country'];
//$d_state_prov = $_REQUEST['d_state_prov'];
//$d_zip = ereg_replace("[^0-9a-zA-Z]","", $_REQUEST['d_zip']);
//$d_phone_1 = ereg_replace("[^0-9]", "", $_REQUEST['d_phone_1']);
//$d_email = $_REQUEST['d_email'];
//$franchise_id = $_SESSION['franchise_id'];
//$username = $d_email;
//$password = $d_phone_1;
//$website = $_REQUEST['website'];






//$SQL = "INSERT INTO dealers (franchise_id, name, address, city, state, zip, country, website, created, modified)
//   VALUES ('$franchise_id','$dealer_name','$d_address','$d_city','$d_state_prov','$d_zip','$d_country','$website',NOW(),NOW())";


//$result = mysql_query($SQL) or die('Query failed: ' . mysql_error());  



//$dealer_id = mysql_insert_id();


//$SQL = "INSERT INTO login (franchise_id,dealer_id,department_id,first,last,email,username,password,created,modified)
//   VALUES ('$franchise_id','$dealer_id','1','$first_name','$last_name','$d_email','$username','$password',NOW(),NOW())";
//$result = mysql_query($SQL) or die('Query failed: ' . mysql_error());  




// Redirect back to add_staff.php


//header('Location: add_staff.php?m=1') ;



for( $i = 0; $i < count($_REQUEST['phone']); $i++ )
{
  echo(" $_REQUEST['type'][$i] = $_REQUEST['phone'][$i] <br>");
}



?>

Link to comment
Share on other sites

I'm going to assume you are getting a blank page, not because of the level of error reporting, but simply because the $_REQUEST super global array is in fact empty (or at the very least, does not contain the key's that you are trying to output, but that should give an error/warning about undefined index values or some such). as you arn't attempting to send any information to the screen other than what would/should/could be in this array. try my suggestion above and let us know what you get back.

Edited by Muddy_Funster
Link to comment
Share on other sites

Here is the output : array(0) { }

 

and here is the code from previous page

 

<?php


include("include/header.php");
include("include/top_menu.php");
include("include/left_menu.php");


?>










<div class="maincontent">

   <div class="breadcrumbs">
    <a href="dashboard.html">Dashboard</a>
       <span>Add Staff</span>
   </div><!-- breadcrumbs -->


   <div class="left">

    <h1 class="pageTitle">Add Staff</h1>

       <form action="process_add_staff.php" method="post">

        <div class="form_default">
               <fieldset>
                   <legend>Add Staff</legend>
 <p>
                    <label for="occupation">Choose Dealer</label>
                       <select name="dealer_id">
                        <option>Choose Dealer</option>

   <?php
   // Load The dealers in for the franchisee


   include("include/db_connect.php");


   //SESSION_START();

   $franchise_id = $_SESSION['franchise_id'];

   // Query the database and get dealers


   $SQL = "SELECT * FROM dealers WHERE franchise_id = '$franchise_id' ORDER BY name ASC";
   $result = mysql_query($SQL) or die(mysql_error());	  

   while($run = mysql_fetch_array($result))
   {
    ?><option value="<?php echo $run['id']; ?>"><?php echo $run["name"]; ?></option>	  

   <?php

   }


   ?>
                       </select>                    
 </p>
                   <p>
                    <label for="name">First Name</label>
                       <input type="text" name="first_name"  class="sf" />
                   </p>


                   <p>
                    <label for="name">Last Name</label>
                       <input type="text" name="last_name"  class="sf" />
                   </p>
 <p>
                    <label for="department">Department</label>
                       <select name="department">
                        <option>Choose Department</option>

   <?php
   // Load The departments in
   // Query the database and get dealers


   $SQL = "SELECT * FROM departments WHERE franchise_id = '$franchise_id' ORDER BY department ASC";
   $result = mysql_query($SQL) or die(mysql_error());	  

   while($run = mysql_fetch_array($result))
   {
    ?><option value="<?php echo $run['id']; ?>"><?php echo $run["department"]; ?></option>		
   <?php	  
   }


   ?>
                       </select> 

 </p>
 <p class="number">
                     <label for="name">Phone Number</label>
                        <input type="text" name="phone[]"  class="sf" />
                        <select name="type[]">
                          <option value="1">Office</option>
                          <option value="2">Cell</option>
                          <option Value="3">Fax</option>
                        </select>
  <a href="javascript:void();" class="add_phone">Add More</a>


  </p>
                   <p> 
                    <label for="name">Phone Number</label>
                       <input type="text" name="phone_1"  class="sf" />
                   </p>                    
                   <p>
                    <label for="email">Email</label>
                       <input type="text" name="email"  class="sf" />
                   </p>              
                   <p>
                    <button>Submit</button>
                   </p>

               </fieldset>
           </div><!--form-->


       </form>

       <br />



   </div><!--fullpage-->

   <br clear="all" />

</div><!--maincontent-->


<?php


include("include/footer.php");


?>

Edited by snowdog
Link to comment
Share on other sites

Think I might have found it. I had Session_start() turned off on the input page and now I am getting this.

 

array(7) { ["dealer_id"]=> string(2) "17" ["first_name"]=> string(5) "James" ["last_name"]=> string(8) "Clausner" ["department"]=> string(1) "3" ["phone"]=> array(1) { [0]=> string(10) "4164536935" } ["type"]=> array(1) { [0]=> string(1) "2" } ["email"]=> string(15) "james@james.com" }

Link to comment
Share on other sites

ok so the values are coming through properly now. I have eliminated everything out of the process page but the array loop and am getting no output :( . My guess is the area of problem is in the loop where I am trying to count the number of rows in the array.

 

<?php
include("include/db_connect.php");

SESSION_START();

for( $i = 0; $i < count($_POST['phone']); $i++ )
{
echo(" $_POST['type'][$i] = $_POST['phone'][$i] <br>");
}
//var_dump($_POST);
?>

Edited by snowdog
Link to comment
Share on other sites

I have the right information coming through, the right count is there. But as soon as I uncomment the for loop it gives me the white screen of death. I can't find the stupid error.

 

Here is the output

 

array(7) { ["dealer_id"]=> string(2) "16" ["first_name"]=> string(5) "James" ["last_name"]=> string( "Clausner" ["department"]=> string(1) "3" ["phone"]=> array(3) { [0]=> string(10) "4168887467" [1]=> string(10) "9058336888" [2]=> string(10) "4164536935" } ["type"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "3" [2]=> string(1) "2" } ["email"]=> string(15) "james@james.com" } 
COUNT: 3

 

Here is the for loop code

 

var_dump($_POST);
$array_count = count($_POST['phone']);
echo("<br><br>COUNT: $array_count ");
for ($i = 0, $i < $array_count; $i++)
{
 echo(" $i  <br>");
}




?>

Link to comment
Share on other sites

ok now have gotten further, The error was a comma instead of a semi colon after $i=0. Now I am getting this

 

array(7) { ["dealer_id"]=> string(2) "16" ["first_name"]=> string(5) "James" ["last_name"]=> string( "Clausner" ["department"]=> string(1) "3" ["phone"]=> array(2) { [0]=> string(10) "4168887467" [1]=> string(12) "905-833-6888" } ["type"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["email"]=> string(15) "james@james.com" }
COUNT: 2

The array row is: 0
The array row is: 1

 

but as soon as I add in, get white screen

var_dump($_POST);
$array_count = count($_POST['phone']);
echo("<br><br>COUNT: $array_count <br><br><br>");
for ($i = 0; $i < $array_count; $i++)
{
echo("The array row is: $i <br>");
echo(" $_POST['type'][$i] = $_POST['phone'][$i] <br>");
}


Edited by snowdog
Link to comment
Share on other sites

snowdog: In case you're still struggling with understanding arrays, think of them as a sheet of paper.

1 dimensional arrays ($array[X]) are like a lined paper, on which each line can have a different value.

2 dimensional arrays ($array[X][Y]) are like a squared (math) paper, where each box can have a different value.

3 dimensional arrays will be like a stack of squared paper, where Z denotes the position of the sheet from the top.

 

You can even picture them as a shelf, if you'd like. The point is that an array is just a collection of compartments, that each hold a single value. The tricky part is that said value can also be an array, creating the multiple dimensions. However, if you keep the mental model simple and consistent, it should be very easy to understand them. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.