Solved

Array's

Posted on 2013-12-06
28
250 Views
Last Modified: 2013-12-26
From what I've read the following code should work.

It's blowing up on the line that's in bold.  I'm sure it's something stupid.

----------------------------------------------------------------------------------------------

$last_bldg_seq = "";
                        
                        foreach ($even_array as $display_array)
                        {
                              
                        // ****************************************************
                        // * If new building change color background          *
                        // * and save building number.                        *
                        // ****************************************************
                              if ($last_bldg_seq == "")
                              {
                                    $last_bldg_seq = $display_array['BNMB'];
                              }
                              if ($last_bldg_seq != $display_array['BNMB'])
                                    {
                                          $count++;
                                          $current_store_count = 0;
                                          if ($bgcolor == "F6F7D7")
                                          {
                                                $bgcolor = "DCF7DC";
                                          }
                                          else
                                          {
                                                $bgcolor = "F6F7D7";
                                          }
                                    }
                              $last_bldg_seq = $display_array['BNMB'];

---------------------------------------------------------------------------------------------
Thanks
Glenn
0
Comment
Question by:breeze351
  • 9
  • 8
  • 4
  • +2
28 Comments
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 39701959
is BNMB exactly what the key is for what you are trying to retrieve?
If it is something like bnmb or Bnmb it would not be found by $display_array['BNMB'].
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39701972
What does exactly mean 'blowing up'?
0
 

Author Comment

by:breeze351
ID: 39701986
"BNMB" is the key
Here's the array.
------------------------------------
$even_array = array(
                  "BNMB" => "",
                  "STRT" => "",
                  "STOREKEY" => "",
                  "SEQ" => "",
                  "FRONTAGE" => ""
                  );
-------------------------------------

By blowing up I mean here's the error message:

----------------------------------------------
Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 350
-------------------------------------------------

 Line 350 is:
--------------------------------------------------
$last_bldg_seq = $display_array['BNMB'];
------------------------------------------------------
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 39702004
you are iterating through $even_array  here foreach ($even_array as $display_array)
which means that $display_array will equal what ever is in each array location and will have nothing in $display_array['BNMB'] because in the array you showed us no array location contains another array.
If you want to know what is in the location keyed with BNMB try $even_array['BNMB'] instead or if you still need to iterate through the array try below.

foreach ($even_array as $key=> $display_array)
{
if($key == "BNMB")
{
 if ($last_bldg_seq == "")
                              {
                                    $last_bldg_seq = $display_array;
                              }
}
}

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702037
IMHO your loop doesn't make sense. Try to replace it with the following

$last_bldg_seq = "";

foreach ($even_array as $key=>$value)
{
	  
// ****************************************************
// * If new building change color background          *
// * and save building number.                        *
// ****************************************************
	if ($key == "BNMB")
	{ 
	  if ($last_bldg_seq == "")
	  {
			$last_bldg_seq = $value;
	  }
	}
	if ($last_bldg_seq != $value)
	{
		$count++;
		$current_store_count = 0;
		if ($bgcolor == "F6F7D7")
		{
			$bgcolor = "DCF7DC";
		}
		else
		{
			$bgcolor = "F6F7D7";
		}
	}
    $last_bldg_seq = $value;
}

Open in new window

0
 

Author Comment

by:breeze351
ID: 39702058
Ok, let me get this correct.

1. I'm iterating through "$even_array".
2. If I define "$display_array" with the same variables, the code will work?
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702086
I'll try to be clear. Your original code would work if the values of $even_array were arrays, taht is if $even_array were an array of arrays:

$even_array = array
(
	[0]=>array(
		"BNMB" => "some1",
		"STRT" => "",
		"STOREKEY" => "",
		"SEQ" => "",
		"FRONTAGE" => ""
	),
	[1]=>array(
		"BNMB" => "some2",
		"STRT" => "",
		"STOREKEY" => "",
		"SEQ" => "",
		"FRONTAGE" => ""
	),
);

Open in new window


In this case your code would work.

Now, you can call your array as you wish, but I don't understand your question "If I define "$display_array" with the same variables, the code will work?" Maybe you can post an example of what you mean.
Sure you can't iterate an array as you did if that array is not an array of array.
Wait for example...
0
 

Author Comment

by:breeze351
ID: 39702090
There are more than 1 variables in the array.
I'm confused on this line:
-------------------------------------------------------
foreach ($even_array as $key=>$value)
-------------------------------------------------------
Does $value contain the variable names?
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 39702099
$key contains the name of the key of the current record of the array that you are looping through.
$value contains the value of the current record of the array that you are looping through.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702109
In your case $key holds "BNMB", "STRT", "STOREKEY", "SEQ", "FRONTAGE" whereas $value holds empty strings.
0
 

Author Comment

by:breeze351
ID: 39702129
Marquas:

"$even_array" has the data and it does display in a test.  I need to iterate through it in a second part of the code.

If you would like to see what I'm trying to do (I'm not worried about it, it's test data):

Site: langsystems.net
Login: sales
ID: lsi

Click on "Walk By"
Address: 575
Street: lex

I've populated two arrays (left and right) on the top of the page.  Now I'm trying to display them under the header for the crossing street (East 42nd),
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702137
Suppose this is your array:

$even_array = array(
                  "BNMB" => "BNMB value",
                  "STRT" => "STRT value",
                  "STOREKEY" => "STOREKEY value",
                  "SEQ" => "SEQ value",
                  "FRONTAGE" => "FRONTAGE value"
                  );

Basically, you can iterate an array in three ways:

1) for loop

for ($i = 0; $i < count($even_array); $i++){
    echo $even_array[$i] . "\n";
}

Open in new window


The ouput is:

BNMB value
STRT value
STOREKEY value
SEQ value
FRONTAGE value

2) foreach equivalent
foreach ($evan_array as $value){
    echo $value . "\n";
}

Open in new window


The output is identical:

The ouput is:

BNMB value
STRT value
STOREKEY value
SEQ value
FRONTAGE value

3) foreach associative style

foreach ($evan_array as $key=>$value){
    echo $key . " = " . $value . "\n";
}

Open in new window


The ouput is:

BNMB = BNMB value
STRT = STRT value
STOREKEY = STOREKEY value
SEQ = SEQ value
FRONTAGE = FRONTAGE value

Hope this is clear enough :)
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702150
Well, I toke a look, but I need the code. I think my explanation should help you to build correclty the page, but if you need a deeper help, post the whole code here, please.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39702226
$even_array["BNMB"] is the value you are looking for.  The 'foreach' is doing nothing for you.  Unless you have more than one $even_array, you do not need to 'iterate' through it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:breeze351
ID: 39702280
Okay.
This code works.

-------------------------------------------
foreach ($even_array as $display_key=>$display_value)
                        {
------------------------------------------

Last stupid question.

I need the value from a single key. What is the syntact?

------------------------------------------------
echo "<td height = ";
echo $display_key['FRONTAGE']['Frontage'];
-------------------------------------------------

Frontage tells me the height of the row.

Thanks
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 39702286
if $even_array is an array of arrays that match the format that you showed earlier then all you need is $display_value['FRONTAGE'] while in your foreach.
$display_key is the key to the current array record not an array itself.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39702305
In case you are wondering about what a particular PHP function does, you can easily look it up on PHP.net.  This is one of the greatest features of PHP.  I always work with a window open to the online manual.  Example:
http://php.net/foreach

Most of the descriptions include examples and user-contributed notes which are (mostly) correct usages of the functions.  The online man page is required reading before you use any PHP function!

I looked at the site, and this is what I'm seeing on the page.  Can you please tell us what these fields are named and what they mean?

Even_Array_Count = 3
Even_Bldg_Count = 0
Odd_Array_Count = 6
Odd_Bldg_Count = 1

Even Numbers
590^Lexington Avenue^1001^60^Scudder Funds Center^
590^Lexington Avenue^1002^100^345 Park Avenue Back Entrance^
590^Lexington Avenue^1003^40^Le Relais de Venise L'entrecote^
0^200^3^

Odd Numbers
575^Lexington Avenue^1001^20^Duane Reade^
575^Lexington Avenue^1002^20^New York Sports Club (2nd Floor)^
575^Lexington Avenue^1003^20^Building Entrance^
575^Lexington Avenue^1004^60^Camera Land^
591^Lexington Avenue^1001^20^Jean Louis David Hair Salon^
591^Lexington Avenue^1001^60^Oxford Cafe^
0^120^4^
1^80^2^

Open in new window

I am guessing as I look at this that you might find a huge lightbulb would go on if you made an array of objects.  Maybe I can post an example of how that might look.

Also, you might want to take a little while to create a template with valid markup.  The document is not going to work well in its current state.
http://validator.w3.org/check?uri=http%3A%2F%2Flangsystems.net%2FWalk_By_Map.php&charset=%28detect+automatically%29&doctype=Inline&group=0
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702330
The syntax is:

echo "<td height = ";
echo $display_key['FRONTAGE'];
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39702333
I reccommend you to read this book http://www.sitepoint.com/books/phpmysql5/. It can help you to understand many thing about Php and how Php and MySql work together.
0
 

Author Comment

by:breeze351
ID: 39702345
I tried what you said.  I'm getting an error on lines 380 and 390:

Errors:
 Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 380

Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 390

Code:
----------------------------------------------------
// ****************************************************
                  // * Create table row for listings.                   *
                        // ****************************************************

                              echo '<tr>';
                                    echo '<td height = "';
                                    echo $display_value['FRONTAGE'];
                                    echo '" bgcolor = "';
                                    echo $bgcolor;
                                    echo '" style= "text-align:left"';
                                    echo ' font color= "blue">';
                                    
                        // ****************************************************
                  // * Display tenant names.                            *
                        // ****************************************************

                                    echo $display_value['TENANT'];
                                    echo "</td>";
--------------------------------------------------------------------
Lines 380 & 390 are bold and underlined.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39702361
When I run this line of code,
echo $display_value['FRONTAGE'];

I get this:
Notice:  Undefined variable: display_value in /public_html/RAY_temp_breeze351.php on line 5

When I run these two lines of code,
$display_value = array();
echo $display_value['FRONTAGE'];


I get this:
Notice:  Undefined index: FRONTAGE in /public_html/RAY_temp_breeze351.php on line 6

When I run these two lines of code,
$display_value = '';
echo $display_value['FRONTAGE'];


I get two messages, like this:
Warning:  Illegal string offset 'FRONTAGE' in /public_html/RAY_temp_breeze351.php on line 6
Notice:  Uninitialized string offset: 0 in /public_html/RAY_temp_breeze351.php on line 6

Could it be possible that you're getting two messages and only telling us about one of the messages?

Also, I second what @marqusG said about the SitePoint book.  It's a good place to start learning this technology.  You would probably want to take a step further back, and pick up the basics of HTML and CSS.  SitePoint has books for that, too.  For PHP, I also like this one: http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39702363
You keep trying to treat $display_value as an array and it is Not.  It is simply a value.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39702383
Have a look at this.  It's amazingly simple to use an array of objects.  The syntax is much simpler than associative array keys, and I find it easier to keep the logic correct.  These arrays are easy to sort on any property of the object with usort().  There are just a lot of advantages to this kind of data structure!
http://www.laprbass.com/RAY_temp_breeze351.php

<?php // RAY_temp_breeze351.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28312538.html

/* BACKGROUND INFORMATION - REQUIRES CLARIFICATION

BNMB
STRT
STOREKEY
SEQ
FRONTAGE

Even_Array_Count = 3
Even_Bldg_Count = 0
Odd_Array_Count = 6
Odd_Bldg_Count = 1

Even Numbers
590^Lexington Avenue^1001^60^Scudder Funds Center^
590^Lexington Avenue^1002^100^345 Park Avenue Back Entrance^
590^Lexington Avenue^1003^40^Le Relais de Venise L'entrecote^
0^200^3^

Odd Numbers
575^Lexington Avenue^1001^20^Duane Reade^
575^Lexington Avenue^1002^20^New York Sports Club (2nd Floor)^
575^Lexington Avenue^1003^20^Building Entrance^
575^Lexington Avenue^1004^60^Camera Land^
591^Lexington Avenue^1001^20^Jean Louis David Hair Salon^
591^Lexington Avenue^1001^60^Oxford Cafe^
0^120^4^
1^80^2^
*/

// AN OBJECT TO CONTAIN THE INFORMATION
Class Thing
{
    // CONSTRUCTOR CREATES CLASS PROPERTIES FROM THE ARGUMENTS
    public function __Construct($a, $b, $c, $d, $e)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
        $this->d = $d;
        $this->e = $e;
    }
}

// CREATE A TEST DATA SET - TWO ARRAYS OF OBJECTS
$evens = array
( new Thing('590', 'Lexington Avenue', '1001',  '60', 'Scudder Funds Center')
, new Thing('590', 'Lexington Avenue', '1002', '100', '345 Park Avenue Back Entrance')
, new Thing('590', 'Lexington Avenue', '1003',  '40', 'Le Relais de Venise L\'entrecote')
)
;
$odds = array
( new Thing('575', 'Lexington Avenue', '1001',  '20', 'Duane Reade')
, new Thing('575', 'Lexington Avenue', '1002',  '20', 'New York Sports Club (2nd Floor)')
, new Thing('575', 'Lexington Avenue', '1003',  '20', 'Building Entrance')
, new Thing('575', 'Lexington Avenue', '1004',  '60', 'Camera Land')
, new Thing('591', 'Lexington Avenue', '1001',  '20', 'Jean Louis David Hair Salon')
, new Thing('591', 'Lexington Avenue', '1001',  '60', 'Oxford Cafe')
)
;

// SHOW HOW TO FIND THE INTERSECTIONS OF THE EVEN AND ODD ELEMENTS ON PROPERTY "c"
foreach ($evens as $even)
{
    foreach ($odds as $odd)
    {
        if ($even->c == $odd->c)
        {
            echo PHP_EOL;
            echo $even->c;
            echo " MATCHES FOUND AT ";
            echo "Even: $even->e, ";
            echo "Odd: $odd->e ";
        }
    }
}

Open in new window

0
 

Author Comment

by:breeze351
ID: 39702467
Dave:

I know I'm going about this in the wrong way, otherwise I could have figured it out.

I have a populated array called "$even_array".  This array contains all the buildings and stores that have an even number on one side of the street.  The array works for both sides of the street (even and odd (that's another array)).

On the even side of the street there could be anywhere from 1 to 100 addresses.  In each address there could be from 1 to 100 stores ( the 100 is silly, but you get my drift).

Each store has a frontage that I need to use to determine the row height as well as the tenant name.  This data goes into a table.

I want to iterate through it without having to keep a counter for the number of buildings and the number of stores in each building.

I've changed the code to read:
------------------------------------------------------------------------
foreach ($even_array as $display_key=>$display_value)  
------------------------------------------------------------------------

Since the array has multiple variables, what is the syntax to display an individual one?

Telling me to by a book is no help.  I'm sitting here with "PHP & MYSQL Novice to Ninja" and the code I've written came from there.

Thanks
Glenn
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39702551
Are there any community colleges in your area?  You may be able to find one that offers classes in PHP programming.  Structured learning (progressively challenging examples in a spiraled curriculum) will give you a lot more value than just copying code and hoping that it works.  You might be able to get access to teachers and advisors.  There is no shortcut to learning any programming language (or any human language).  But it can be made more efficient with a classroom environment.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39702660
I have a populated array called "$even_array".  This array contains all the buildings and stores that have an even number on one side of the street.
Nothing that you have posted shows that.  All you have posted is $even_array with 5 separate values and None of them are arrays.  Which is why you keep getting errors when you try to access them as arrays.

Can you show me a copy of $even_array that actually contains data for ALL the buildings and not just one?
0
 

Accepted Solution

by:
breeze351 earned 0 total points
ID: 39706874
Ok, check the web site again.  This time look at the "Odds"  All the data should be there.

Sorry that I haven't got to this sooner.  My mother-inlaw has been in out of the hospital for the last 5 days.  I've running around like a nut.  There's nothing you can do because it's your wife's mom.  So if the wife says "You have to do this!" , there's no answer but "Yes dear".

Glenn
0
 

Author Closing Comment

by:breeze351
ID: 39739806
Got it
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now