Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populating arrays

Posted on 2013-11-16
11
Medium Priority
?
159 Views
Last Modified: 2014-03-27
This is a stupid question.  I'm sure somebody can answer it in a second.
Here's the code:

$right_array = array(
                  "BNMB" => "",
                  "STRT" => "",
                  "STOREKEY" => "",
                  "SEQ" => ""
                  );
$right_array_count= 0;
while ($ROW = Mysql_fetch_assoc($mapfile, MYSQL_BOTH))
      {
            if ($ROW['BNMB'] % 2 == 0)
            {
                  $fred = "It's even";
                  $left = "N/A";
                  $right = $right_array_count;
*****                  $right_array[$right_array_count] =   *****
                  $right_array_count = ($right_array_count + 1);

I need to populate the "right_array" with the vars from the query.  Everything on the net is either about an array with no multiple vars or 2 dimensional arrays.

I just need the syntax.

Thanks
Glenn
0
Comment
Question by:breeze351
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39653820
Yes.  Your array is not organized by numbers but text keys:  "BNMB", "STRT", "STOREKEY", "SEQ".  It must be accessed with those keys, not by numbers.

$right_array["BNMB"]
$right_array["STRT"]
$right_array["STOREKEY"]
$right_array["SEQ"]
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39653860
Fortunately all of the PHP functions are documented on the PHP.net web site!

mysql_fetch_assoc()
mysql_fetch_array()

If you're not 100% sure what a PHP function does, you can look it up at the click of a mouse.

If you're new to PHP and want to learn more about how to use the language, this article may help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:breeze351
ID: 39653977
Dave:
I think I'm going about this the wrong way or I'm explaining it wrong.  The language that I am familiar with deals with arrays in a different way.  I can read a file and store variables in the array.  Each record in the data file will have a different line in the array.

What I'm trying to do is:
1) Query the data base for buildings.
2) Store the info that I need into 2 arrays based on odd or even building #.
3) When the query is over, display the odd #'s on the left and the even #'s on the right.

Don't I have to tell PHP what line to insert into the array?  Or is an array in PHP only 1 line?

I just thought of something, is what I'm trying a 2 dimensional array in PHP that has to be defined as such?
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!

 

Author Comment

by:breeze351
ID: 39653981
Ray:
Thank you for your reply.
However, all the links gave me are "DEPRECATED"!!!
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39654009
You are going about it wrong.  Arrays in PHP do not have to have numbered elements.  They can be text 'keys' instead.  This page http://us1.php.net/manual/en/language.types.array.php tells you all about it.

And $ROW['BNMB'] is one element in the array $ROW.  Note that variable names are case sensitive in PHP.  And on Linux, the column names are case sensitive also.

And the 'mysql' functions are Deprecated.  'mysqli' has been the preferred driver for about 6 years.  However, you are probably not using PHP 5.5.0 so you don't have to worry about it yet.
0
 

Author Comment

by:breeze351
ID: 39654023
Dave:
This code works:

while ($ROW = Mysql_fetch_assoc($mapfile, MYSQL_BOTH))
      {
            if ($ROW['BNMB'] % 2 == 0)
            {
                  $fred = "It's even";
                  $left = "N/A";
                  $right_array[$right_array_count]["BNMB"] = $ROW['BNMB'];
                  $right_array[$right_array_count]["STRT"] = $ROW['STRT'];
                  $right_array[$right_array_count]["STOREKEY"] = $ROW['STOREKEY'];
                  $right_array[$right_array_count]["SEQ"] = $ROW['SEQ'];
                  $right_array_count = ($right_array_count + 1);
                  $right = $right_array_count;
            }
            else
            {
                   $fred = "It's odd";

...................

$count = 0;
echo "Right<br>";

      while ($count < $right_array_count)
      {
            echo
                  $right_array[$count]['BNMB']."^".
                  $right_array[$count]['STRT']."^".
                  $right_array[$count]['STOREKEY']."^".
                  $count.
                  "<br>";
            $count++;
      }

.................

Is something wrong with this?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39654049
Technically no but it does Not match the $right_array variable in your original post.  I suggest you do a 'var_dump' on all three versions so you can see the difference.  In your original post, you would have $right_array["BNMB"].  In the code above, you would have $right_array[0]["BNMB"].
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39654495
Glad you noticed that MySQL is deprecated!  I was afraid you had not read the manual for those functions since you posted this:

Mysql_fetch_assoc($mapfile, MYSQL_BOTH))

When you get ready to convert to the current DB extensions, this article will help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you want to show us the query statement and a sample of the test data, we might be able to help you get the data you want more directly, without all of the PHP code.   Just a thought... ~Ray
0
 

Author Comment

by:breeze351
ID: 39654892
Dave:

This code does work.  Check it out.  I'm not worried about giving the login as this is only test data.

Site: langsystems.net
User: sales
Password: lsi

Click "Walk By Map"
The address is "575" "lex"

When you look at this, you'll probably see what I'm trying to do.

Glenn
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 39654902
That's nice, Glenn, but that is different than your original question.   That's all I was saying.
0
 

Author Closing Comment

by:breeze351
ID: 39655427
Thanks Dave
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

650 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