[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Php Syntax

I'm sure this is syntax error in "Space_Display.php".  

I have several pages that call "Print_Formats.php"  so that the user can select what format they want to use.  It works when calling from "Multi_Space.php", however when it returns from a page called "Space_Display.php" I get the following error:

<b>Notice</b>:  Undefined offset: 1 in <b>/home/mrbreeze/public_html/chris/Space_Display.php</b> on line <b>227</b><br />

When "Space_Display.php" is originally loaded it works. When it comes back I get the above error.

This is the code from "Space_Display.php"

               $date_len = strlen("$ROW[LISTING_DA]");
               if ($date_len < 6)
                     {
                        $ROW['LISTING_DA'] = "0".$ROW['LISTING_DA'];
                  }
               $date = str_split("$ROW[LISTING_DA]",2);
               echo "$date[0]/$date[1]/$date[2]";
               echo "<br>";

The bold line is 227.

When the user clicks on the "Print Format" button, it calls "Print_Format.php". "Print_Format" calls "Print_Format_Return.php".   This is the code from "Print_Format_Return.php".

  <?php
session_start();
$_SESSION['P_Format'] = $_POST['Print_Format_Radio'];
header('Location: ' . $_POST['referrer']);
?>
0
breeze351
Asked:
breeze351
  • 9
  • 4
  • 2
  • +3
1 Solution
 
asavahCommented:
Provide full Space_Display.php, from the sample you provided it's unclear what happens with $ROW[LISTING_DA] from the error you're getting and the info you provided I can only say that  on the second call $ROW[LISTING_DA] length is less or equal than 2, probably it's null, str_split() is returning only one element of the array date[] - date[0].
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Try adding single quotes around the associative array index in the line before (226).
$date = str_split("$ROW['LISTING_DA']",2);

Open in new window

0
 
breeze351Author Commented:
I've tried both solutions.  Neither one works.

If you'ld like to try it:

1:  Go to "chris.mrbreeze.net".  
2:  The login is "sales","lc".  
3:  "Search Buildings" The address is "362" "2nd".
4:  Click "Print Format".  Change it to what ever and hit select.
5:  The page returns to "Multi_Spaces.php" with the correct display.
6:  Hit the "Display" button.
7:  Repeat steps 4 and 5

This is will blow it up.

This only test data so I have no problems with this.

Glenn
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kim WalkerWeb Programmer/TechnicianCommented:
Unfortunately, we'd need to see your php code in order to troubleshoot. My best guess is that $ROW['LISTING_DA'] does not contain what you expect to be there. It likely only has a single zero in it. Try changing the if to a while:
 while ($date_len < 6)
                     {
                        $ROW['LISTING_DA'] = "0".$ROW['LISTING_DA'];
                  }

Open in new window

This will result in a string of six zeros if the value from the database is empty and should prevent the error you're getting. It may generate a different error, though.
0
 
Ray PaseurCommented:
If you're doing date formatting by manipulating strings, you're making an enormous amount of work for yourself.  This is covered in PHP by two functions that every programmer should know and love.  Look up date() and strtotime() in the PHP.net web site.  This article may be helpful to you, too.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

When you're unsure what is in a variable, var_dump() is the function to use.
http://us3.php.net/manual/en/function.date.php
http://us3.php.net/manual/en/function.strtotime.php
http://us3.php.net/manual/en/function.var-dump.php
0
 
breeze351Author Commented:
If the code works on the 1st load of the page, why wouldn't it work when

header('Location: ' . $_POST['referrer']);

returns to it?

Have you tried the site?
0
 
Ray PaseurCommented:
Right before line 227 insert this:
var_dump($ROW[LISTING_DA]);

You might also want to look at this:
var_dump($ROW);

I can't be sure of this, but my instincts tell me there is a logic error in there somewhere and the contents of $ROW or perhaps $ROW[LISTING_DA] are getting overwritten.
0
 
breeze351Author Commented:
Otay Spanky!

I figured out what it's doing. Why I don't know.

If you login in (see above) and search for a building address.  If there are multiple spaces and the page calls "Multi_Space.php", then you display one space, go to the print format php screen and return, it blows up.  "Multi_Space.php" can call the same page to select a print format and return with no problems.

If you select a building with only one space (50 Greene) and then select the print format it returns correctly.

I tried the var dump and in the 2nd scenario I'm not getting anything back.  

Could this be the
header('Location: ' . $_POST['referrer']);

I thought I read some where this refer may not be set all the time in php.

Glenn
0
 
Mark BradyCommented:
The problem is more than likely a logic error and the quickest way for you to get this solved is to post your code so we can read it and find the error. I have never used $_POST['referrer'] as there are better (more reliable) methods to redirect your page.

The error you are getting is directly related to a pointer to an array that doesn't exist (on the 2nd load). Reading your code will aide in solving this for you.
0
 
rinfoCommented:
you can easily debug it
Just place a
echo$ROW[LISTING_DA];echo '<br>';
and place a line
print_r($date)
after  this line
$date = str_split("$ROW[LISTING_DA]",2);
and you would know what exactly is there in $date and $ROW[LISTING_DA]
0
 
breeze351Author Commented:
elvin66:

I can give you the code but there are about 4 pages.  Some times it works and some times it doesn't.

Have you tried the site?  The login and the other info is above.

I'll try to explain the problem better and what results that I am getting.

Login and go to "Search Buildings"

Scenario #1:
The address is "362" "2nd".  If you notice the "Print Format" has a 1 behind it.
Click the "Print Format" button and select what ever you want.
When you click "Select" from the "Print Format" page, it returns to "Multi_Space.php" with the correct data.  However, when you click on "Display" and then click on "Print Format" and then click on "Select" it blows up on the return.

Scenario #2:
The address is "50" "Greene".  If you click on "Print Format" and the "Select" button it does return to "Space_Display.php" with the correct info.

What I'm trying to do is be able to call the page "Print_Format.php", select a button and then return to the original page.  I was told to create a 2nd page that "Print_Format.php" would call and use $_POST['referrer']

You said that you had a better idea.  What is it

Thanks
Glenn
0
 
Mark BradyCommented:
Ok I went to your site and tried it. Once again it would be easier if we could read the scripts but at least post this one  - Space_Display.php. That is the page that redirects back to Multi_Space.php so chances are, there is something not not right with Space_Display.php. Are you using php sessions to track your variables across multiple pages? If not then you should do. Double check exactly what variables you are sending by POST to the print format page and double check what you are sending back and how. There might be something missing in the results from the 2nd search that is present in the first search.

It really could be a number of things but without seeing the whole code I can't tell.
Just remember, php doesn't "sometimes work and sometimes not". It is always going to be a coding issue rather than a php bug (unless you are extremely unlucky I guess).
0
 
breeze351Author Commented:
I need to diagram the pages for you so that you can see the flow.
How would you like this done (Word)?
Glenn
0
 
breeze351Author Commented:
Elvin
Attached is a php showing what I'm trying to do.

The code is using session variables.  The problem I have with this is that I don't know what page is going to call "Print_Formats.php".  "Print_Formats.php" calls "Print_Formats_Return.php" to set the "P_Format" session variable.  

I didn't want to create another session variable just to figure out what page called "Print_Formats.php".

I was told by "Experts" that by calling "Print_Formats_Return.php" from "Print_Formats.php" with the following code:

header('Location: ' . $_POST['referrer']);

that it would return me to the calling page.

My problem is that when "Multi_Space.php" calls "Space_Display.php" and then you select a print format, it blows up.  But if you select "Print_Formats.php" from "Multi_Space.php" and hit select it returns  fine.  

If you select a building with only one listing and it goes directly to "Space_Display.php", it works fine

Try it again.
Multiple Spaces= 362 2nd
Single Spaces = 50 greene

Don't worry about the case or the diff between 2nd and Second, the code takes care of that.

Glenn
Web-Diagram.pdf
0
 
Mark BradyCommented:
Hi Glenn

I don't think that is the best way to achieve your goals. You can't be sure that $_POST['referrer'] is always going to have the referring page in it. Forget about using $_POST anything. What you can do to ensure you get the same variables across multiple pages reliably is this. Now you can put this in a separate php file if you like and include 'thefile.php' in each of the pages you need to track things. You can also just add this code to the top of those pages but here it is.

<?php // this is any php file you need to track and share variables in
// start a session
session_start();
if(is_array($_REQUEST)) {
  foreach($_REQUEST as $key => $value){
    $_SESSION[$key] = $value;
    }
}

Now ANY variable that is posted by POST or GET will be assigned to a session variable automatically. Once the first page (with this code in it) has assigned the posted values to session variables you will not need to re-post those values to any other pages. You simply get and set their values like this.

On each page you should set the referrer name (after the session code I posted above)
$_SESSION['referrer'] = 'Multi_Space.php'; // put this in the Multi_Space.php file.

Do the same on the other pages. This means you are resetting the referrer page each time which should make sure you always have the latest referrer page in you session variable.

Now you header calls can be like this:

header('location: '.$_SESSION['referrer']);

Of course the easiest way to do this is to assign each pagename to a variable in those pages (similar to what I said above)

<?php // somefile.php
$page = 'somefile.php';

header('location: anotherpage.php?page='.$page);

Then you can get the referrer with a GET.

$referrer = $_GET['page'];

Like I said, there are many ways to do this so I hope one of these will help you.
Cheers
0
 
breeze351Author Commented:
Just so that I understand the logic:

Page #1: "Search_Buildings.php"
  Keyin address

Page #2: "Search_Buildings_Chk.php"
    I get the data with a $Post
    If there are multiple spaces we go to "Multi_Space.php"

Page #3: "Multi_Space.php"
    In "Multi_Space.php", I add the code:
         $page = 'Multi_Space.php';
    The first time this page loads, I have to get the $Post data from "Search_Buildings_Chk.php"

Page #4: "Print_Formats.php"
   The user selects a radio button and hits the select button.

Page #5:  "Print_Formats_Return.php"
   I get the value from the $Post variable and set the $Session variable.
   From here, I add the code:
     header('location: anotherpage.php?page='.$page);

My question is at page #3, I have to use a GET statement.  How do I know where I'm coming from?  The data on the 1st load of this page will be in POST.

Tell me if I'm missing something.

Glenn
0
 
Mark BradyCommented:
The whole idea is that you no longer need to worry about whether you are doing a get or a post. My session code I gave you takes care of that. It looks for $_REQUEST[] which is both GET and POST so everytime something is POSTED to any of those pages, they will be added to the $_SESSION[] array. Each one of your pages that either does a POST to another page you need the receiving page to have my session code on it to capture the posted values.

Then when you need to get the latest values you just get it from the SESSION array.

var_dump($_SESSION); // this will show you what is inside the session array.
0
 
breeze351Author Commented:
Okay, I understand the logic, however I might have a problem with this.

In "Search_Buildings.php" the user can key in a street # and street name.  The only variable that is required is the street name.

This page does a $Post to "Search_Buildings_Chk.php". In this page I'm setting session variables to pass to "Multi_Space.php", "Space_Display.php" as well as "Street_Display.php".  

If "Search_Buildings_Chk.php" returns more than one listing in a building it goes to "Multi_Space.php".  If it returns only one listing, it goes to "Space_Display.php", and if the user only keyed in a street name it goes to "Street_Display.php".

Do I add the following to "Search_Buildings_Chk.php":
if(is_array($_REQUEST)) {
  foreach($_REQUEST as $key => $value){
    $_SESSION[$key] = $value;
    }
}

Also another problem is that "Search_Buildings_Chk.php" finds the building ID as well as the space ID.

Is my logic wrong?
Glenn
0
 
breeze351Author Commented:
Got it thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now