Jump to content

Recommended Posts

I am trying to pass an array to a function.  I have tried using the & symbol in front of it in the function call, I have tried declaring it as global, I have tried everything I can think of.  Does anyone have any suggestions?  I'm kind of at a loss right now.  Any help would be much appreciated.  Thanks. 

 

There is quite a bit of code, but I'll try to put only the code that is relevant. 

 

This is the call function on the index.php page: (the $tc is the array)

 

<?php 
if (isset($_POST['save_data']))
insert_row($mail_date, $site_id, &$tc);
?>

 

and this is the code on the functions.php page:(the code used to have sql statements to get the products and times)

 

<?php
function display_enter_maildrop($mail_date, $site_id, $tc=null, $row_id=null){
    echo "<h1><center><b>Mail Drop Vols</b></center></h1>\r";
    echo "<h2><center><b>Enter For:\r";
    echo($mail_date);
    echo "  \r";
    echo get_db_value('mis', 'mis_site', $site_id);
    echo "</b></center></h2>\r";
    echo "<form name=\"datarow\" action=\"index.php\"  method=\"post\" onSubmit=\"return validate_data('datarow')\">\r";
    echo "<input type=\"hidden\" name=\"site_id\" value='$site_id'>\r";
    echo "<input type=\"hidden\" name=\"mail_date\" value='$mail_date'>\r";
    echo "<input type=\"hidden\" name=\"row_id\" value='$row_id'>\r";
    echo "<center>\r";
    echo "<table border=2 bordercolor = \"#00FFFF\" borderColorLight=\"#DEDEDE\" bgcolor= \"#00008B\" colspan=\"1\">\r";



     echo"<tr><td><b><font color=\"#00C0FF\">Product</font></b></td>";
     echo "<td><b><font color=\"#00C0FF\">Time 1</font></b></td>";
     echo "<td><b><font color=\"#00C0FF\">Time 2</font></b></td>";
     echo "<td><b><font color=\"#00C0FF\">Time 3</font></b></td>";
     echo "<td><b><font color=\"#00C0FF\">Time 4</font></b></td>";
     echo "</tr>";


    $products[1] = "product1";
    $products[2] = "product2";
    $products[3] = "product3";
    $products[4] = "product4";
    for($i=1; $i<=4; $i++)
    {  echo "<tr>";
       echo "<td><font color=\"#00C0FF\">";
       echo $products[$i];
       $product_id = "$products[$i]";
       echo "</font></td>";
       if( $tc == null){
       for($i=1; $i<=4; $i++)
       {
       //This will populate the first row if the array is null
       $tc[$product_id][$i] = $i*4;
       }
                       }
       for($i=1; $i<=4; $i++)
       {
        echo "<td colwidth = \"100\"><input size=\"10\" name=\"volume\" type=\"text\" data_type = 'int' value=".$tc[$product_id][$i]."></td>\r";
       }
    }
    $rows++;
    echo "<tr>\r";
    echo "<td colspan=\"$rows\" align = center>\r";
    echo "<input name=\"save_data\" type=\"submit\" value=\"Save\">\r";
    echo "<input name=\"cancel_data\"  type=\"submit\" value=\"Cancel\" onClick='do_not_validate(this)'>\r";
    echo "</td>\r</tr>\r</table>\r</center>\r</form>\r";
     //print_r(&$tc);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/
Share on other sites

change this:

 function display_enter_maildrop($mail_date, $site_id, $tc=null, $row_id=null){

 

to this:

 function display_enter_maildrop($mail_date, $site_id, $tc=array(), $row_id=null){

 

or this should work too:

 function display_enter_maildrop($mail_date, $site_id, $tc, $row_id=null){

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227610
Share on other sites

Right now, I just have this, becuase I'm trying to see if it will even pass the array to here.

 

<?php
     function insert_row($dt, $site_id, $tc) {
     if($tc == null){
      echo "null<br>";
      $tc[3][4] = 12;
      print_r($tc);
     }else{
     display_enter_maildrop($mail_date, $site_id, $tc);
           }
     }
     ?>

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227613
Share on other sites

I also put this above the "insert_row" function:

 

<?php if($tc==null){ echo "null<br> null<br>";} ?>

 

and it echos out the nulls, so it isn't even getting to the index.php page from the functions.php page. 

The other variables are getting there just fine, I can echo out the date or site and they both show up. It is only the array that is having issues. 

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227618
Share on other sites

I have been trying a few different things, and it looks like the only issue is passing the array from the functions.php page to the index.php page.  It will pass the other way just fine.  Not sure if this helps, but that is the only place it doesn't pass.  Thanks again for your time and help.

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227659
Share on other sites

I have been doing that throughout, and that is how I figured out that the only place that it is having issues passing is from the "display_enter_maildrop" function (on the functions.php page) to the index.php page (to pass back to the "insert_row" function).

 

can you elaborate?? that didn't make much sense to people who aren't looking at your screen.

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227673
Share on other sites

I'll try...

 

I have been trying to echo out the array to figure out exactly where it is getting hung up, and there is only one place where I can find that it isn't passing. 

 

The index.php page calls the functions, and if a button is pushed, it calls the function it is supposed to.

First you enter data in the "display_enter_maildrop" function (this function is on the "functions.php" page).

Next you click the "save" button.  On the index page, this button calls the "insert_row" function from the "functions.php" page. 

At this point, the "insert_row" function calls the "display_enter_maildrop" function again.

 

The point where it is not passing is when you press the save button, and it goes to the index page. 

 

I have put code everywhere to insert data into the array, and even on the index page it will pass the array, just not from the save button to the index page. 

 

Hopefully this has clarified things a bit, instead of making them less clear.  Let me know if you need more explination.  Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227695
Share on other sites

The problem is TC is not being passed in as an array. Somewhere, where you are using this function is screwing up.

 

function display_enter_maildrop($mail_date, $site_id, $tc=null, $row_id=null){
    if (!is_array($tc)) {
         DIE("No array was passed in!");
    }

    echo "<h1><center><b>Mail Drop Vols</b></center></h1>\r";
    echo "<h2><center><b>Enter For:\r";
    echo($mail_date);
    echo "  \r";
    echo get_db_value('mis', 'mis_site', $site_id);
    echo "</b></center></h2>\r";
    echo "<form name=\"datarow\" action=\"index.php\"  method=\"post\" onSubmit=\"return validate_data('datarow')\">\r";
    echo "<input type=\"hidden\" name=\"site_id\" value='$site_id'>\r";
    echo "<input type=\"hidden\" name=\"mail_date\" value='$mail_date'>\r";
    echo "<input type=\"hidden\" name=\"row_id\" value='$row_id'>\r";
    echo "<center>\r";

 

Chances are it never gets passed that first if statement. Which means you need to look at where you are passing the array into the function and see if that array is being generated at all.

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227797
Share on other sites

Here is the relevant code for this (and probably a little more). 

 

This is the index.php:

 

<?php
include "db_util.php";
include "functions.php";
require_once "page_functions.php";
include "mis_db_connect.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title>TS Payments Mail Drop Volume</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="HAPedit 3.1">
<script language='javascript' src='../js/validation.js'></script>
<script language='javascript' src='../../lib/calendar/popcalendar.js'></script>
</head>
<center>

<?php
//Mail Drop Index

if ( $_POST==null && $_GET==null){
display_enter_date_site();
}
elseif (isset($_POST['enter_maildrop'])) {
display_enter_maildrop($mail_date, $site_id, $tc=null, $row_id=null);
return;
}

elseif (isset($_POST['save_data'])) {
  if($_POST['row_id']!=null && $_POST['row_id']!=0){
  update_row($mail_date, $site_id, &$tc, $row_id);
  return;
  }else{

//When I enter data for the array here, it pulls back through,
//but it will not pull to here.
insert_row($mail_date, $site_id, &$tc);
return;
}
}
elseif (isset($_POST['cancel_data'])) {
display_enter_date_site($mail_date, $site_id);
return;
}
else{
display_enter_date_site();
}
echo "<br><br><br><br>\r";

?>


</center>

</body>

</html>

</html>

 

And this is the functions.php:

 

<?php
//Mail Drop Functions

function display_enter_date_site($mail_date=null, $site_id=null){
    echo "<body onfocus=\"document.maildropform.site_id.focus();\">";
    echo "<form name=\"maildropform\" action=\"index.php\" method=\"post\" onSubmit=\"return validate_data('maildropform')\">\r";
    echo "<table border=2 bordercolor = \"#00FFFF\" borderColorLight=\"#DEDEDE\" bgcolor= \"#00008B\" colspan=\"1\">\r";
    echo "<thead><tr><th font size = 4 face = \"arial\" colspan=\"2\" bgcolor=\"#C5DBFF\"><b>Mail Drop MIS Entry</b></th></tr></thead>\r";
    echo "<tr><td align=left><font size=\"4\" color=\"#00C0FF\"><b>Site</b></td><td>\r";
    if (!isset($site_id) or $site_id==null)
        $site_id = '4';
    show_db_list('mis','mis_site', 'site_id', $site_id, null, 'where site_id = 2 or  site_id =3 or  site_id =5 or  site_id =4');
    echo "<tr><td align=left><font size=\"4\" color=\"#00C0FF\"><b>Processing Date</b></font>  </td>";
    if (!isset($mail_date) or $mail_date==null)
        $mail_date = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
    echo "<td align=\"left\"><input data_type = 'date_ymd' type=text name='mail_date' size=12 maxlength=11 value='$mail_date'>";
    echo "<script language='javascript'>\r";
    echo "<!--\r";
    echo "if (!document.layers) {\r";
    echo "document.write(\"<input type=button onclick='popUpCalendar(this,maildropform.mail_date, \\\"yyyy-mm-dd\\\")' value='Calendar' style='font-size:12px'>\")}\r";
    echo "//-->\r";
    echo "</script></tr>\r";
    echo "<tr><th font size = 4 face = \"arial\" colspan=\"2\" bgcolor=\"#C5DBFF\"><b>Mail Drop</b></th></tr>\r";
    echo "<tr><th font size=4 color=\"#00C0FF\" colspan=\"2\"><b><input name='enter_maildrop' type=\"submit\" value=\"Mail Drop Entry\"></b></th></tr>\r";
    echo "</table>\r";
    echo "</form>\r";
}
function display_enter_maildrop($mail_date, $site_id, $tc=array(), $row_id=null){

    echo "<h1><center><b>Mail Drop Vols</b></center></h1>\r";
    echo "<h2><center><b>Enter For:\r";
    echo($mail_date);
    echo "  \r";
    echo get_db_value('mis', 'mis_site', $site_id);
    echo "</b></center></h2>\r";
    echo "<form name=\"datarow\" action=\"index.php\"  method=\"post\" onSubmit=\"return validate_data('datarow')\">\r";
    echo "<input type=\"hidden\" name=\"site_id\" value='$site_id'>\r";
    echo "<input type=\"hidden\" name=\"mail_date\" value='$mail_date'>\r";
    echo "<input type=\"hidden\" name=\"row_id\" value='$row_id'>\r";
    echo "<center>\r";
    echo "<table border=2 bordercolor = \"#00FFFF\" borderColorLight=\"#DEDEDE\" bgcolor= \"#00008B\" colspan=\"1\">\r";

    if($site_id == 2)
    {$site = "in_ia";
     $time_zone = "cst_drop";}
    elseif($site_id == 3)
    {$site = "in_nv";
     $time_zone = "pst_drop";}
    elseif($site_id == 5)
    {$site = "in_oh";
     $time_zone = "est_drop";}
    $qry_tz = "select
             $time_zone as time_text
             from mis_mail_drop_time
             where
             $time_zone <> ''
             order by
             time_order";
    //echo $qry_tz;
    $getqry_tz = mysql_query($qry_tz);
     echo"<tr><td><b><font color=\"#00C0FF\">Product</font></b></td>";
    while($row = mysql_fetch_array($getqry_tz))
    {
     echo "<td>";
     echo "<b><font color=\"#00C0FF\">".$row["time_text"]."</font></b>";
     echo "</td>";
    }
     echo "</tr>";
    $qry = "select
             product_text,
             product_id
             from mis_mail_drop_product
             where
             $site = 'yes'";
    //echo $qry;
    $getqry = mysql_query($qry);
    $rows = mysql_num_rows($getqry_tz);
    while($row = mysql_fetch_array($getqry))
    {  echo "<tr>";
       echo "<td><font color=\"#00C0FF\">";
       echo $row["product_text"];
       $product = $row["product_text"];
       $product_id = $row["product_id"];
       echo "</font></td>";
       if( $tc == null){
       for($i=1; $i<=$rows; $i++)
       {$tc[$product_id][$i] = $i*4;}
                       }
       for($i=1; $i<=$rows; $i++)
       {
       echo "<td colwidth = \"100\"><input size=\"10\" name=\"volume\" type=\"text\" data_type = 'int' value=".$tc[$product_id][$i]."></td>\r";
       }
    }
    $rows++;
    echo "<tr>\r";
    echo "<td colspan=\"$rows\" align = center>\r";
    echo "<input name=\"save_data\" type=\"submit\" value=\"Save\">\r";
    echo "<input name=\"cancel_data\"  type=\"submit\" value=\"Cancel\" onClick='do_not_validate(this)'>\r";
    echo "</td>\r</tr>\r</table>\r</center>\r</form>\r";
     print_r(&$tc);
      $tc[3][4] = 188;
}



function insert_row($dt, $site_id, $tc) {
     if($tc == null){
      echo "null<br>";
      $tc[3][4] = 12;}
      // print_r($tc);

     display_enter_maildrop($mail_date, $site_id, $tc);
}
?>

 

There are several places in this code where I insert new numbers into the database.  This was to see where exactly it was passing the array and where it wasn't.

 

Hopefully this will help out more than my poor descriptions.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227826
Share on other sites

I'm trying to pass the array "$tc"

 

The index.php page calls the functions to pop up the page, and if a button is pushed, it calls the function it is supposed to.

 

First you enter data in the "display_enter_maildrop" function (this function is on the "functions.php" page).

 

Next you click the "save" button.  On the index page, this button calls the "insert_row" function from the "functions.php" page. 

 

At this point, the "insert_row" function calls the "display_enter_maildrop" function again.

 

My problem comes in right when you push the save button, the other fields are being passed, but the array is getting lost in cyberspace. 

 

I am trying to figure out how to pass this array.  I have done the print_r($tc) in various places, and that's how I figured out that it is not working at the save button. I have manually inserted numbers into the array at different points also, thus figuring out where it starts passing the values.

 

I can't figure out why it won't pass the array when I press the "save" button. 

Link to comment
https://forums.phpfreaks.com/topic/46718-passing-an-array/#findComment-227893
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.