SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast imedia's Avatar
    Join Date
    Oct 2004
    Location
    new england, usa
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Why does using $count make this work?

    Hi all!

    I'm in desperate need to know how and why my accident worked!

    I've been teaching myself PHP & MySQL to help reduce maintenance time for my websites. Up until now I've been cruising along smoothly. Now I have have an array function I created that works by accident! It took me hours to figure out it was an accident. Understand this is very important to me.

    A few days ago I hit one point where I believed I needed to manipulate some data using arrays. I'm building an event's manager system to automate content about venues and events.

    I want to ask a few questions, but my main concern is why using $count in a foreach loop produces the result I wanted.

    I have a MySQL table containing all kinds of information about venues;
    id, venue_name, street, city, state, zip, mapURI, entered, modified, etc...

    I've gotten past the part of posting and retrieving events with forms... and wanted to display the entire calender of events... and such. The kicker is I don't want to use the traditional HTML table format, which is easy.

    OK, here we go;
    I wanted a 2 col HTML table where the Months are displayed in
    <TH>MONTH_1</TH>
    <TH>MONTH_2</TH>
    with the corresponding events in the <TD> below for each month... so we have:
    Code:
    <table>
    <tr><th>October</th><th>November</th></tr>
    <tr><td>All Oct. Events</td><td>All Nov. Events</td></tr>
    repeat...
    So I figured a few for(), foreach(), and while() loops would work while manipulating the MySQL row data in arrays. Never using arrays before, I spent a few days playing around... and ending up going from this array:
    PHP Code:
    array(8) {
      [
    0]=> array(3) {
        [
    "date_grp"]=> "October"
        
    ["date_col"]=> "2004-10-29"
        
    ["venue_name"]=> "A1 Sports Bar & Grill"
      
    }
      [
    1]=> array(3) {
        [
    "date_grp"]=> "October"
        
    ["date_col"]=> "2004-10-30"
        
    ["venue_name"]=> "The Regatta"
      
    }

    //....truncated...//

      
    [7]=> array(3) {
        [
    "date_grp"]=> "November"
        
    ["date_col"]=> "2005-11-04"
        
    ["venue_name"]=> "The New Eagle Club"
      
    }

    TO using this function:
    PHP Code:
    #$var = $row[] results from a MySQL query
    #The first foreach() loop produces the array shown above.

    //Outputs last event in array//
    function perEvent($var){
        foreach (
    $var as $values){
            
    $eventGroups[] = $values;
        }
        foreach (
    $eventGroups as $count){
            
    $fieldCount[] = $count;
        }
        foreach (
    $count as $n){
            
    $i .= $n."<br>";
        }
        echo 
    $i;

    And getting this HTML output:

    November
    2005-11-04
    The New Eagle Club


    I wrote the function in a quick frenzy while desperately trying to understand how array's work and get my desired output. I forgot that count is a function... If I didn't try to rename the variables I wouldn't have know this was unusual (at least to me).

    Why does this work? The only thing I found refers to constants and it's extremely vague.

    Is it stable?

    If so, can I flip the array (to get the right order of dates), flip it again and then pop off the last array after each instance?

    Is there a better way to capture all the fields in each row without using MySQL_queries for each table cell?

    I'm dying to know!

    Your truly,
    Mr. Fried On PHP

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm ... shouldn't you have $fieldCount as $n in your third loop?
    PHP Code:
    function perEvent($var){ 
        foreach (
    $var as $values){ 
            
    $eventGroups[] = $values
        } 
        foreach (
    $eventGroups as $count){ 
            
    $fieldCount[] = $count
        } 
        foreach (
    $fieldCount as $n){ 
            
    $i .= $n."<br>"
        } 
        echo 
    $i

    What seems to be happening is that after the eventGroups loop, $count is set to the last item in your array.

    Cheers,
    Af.

  3. #3
    SitePoint Enthusiast imedia's Avatar
    Join Date
    Oct 2004
    Location
    new england, usa
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Afro Boy
    Hmmm ... shouldn't you have $fieldCount as $n in your third loop?
    That's how I discovered it worked! I wrote it by accident... when I was ready to move on to the next function, I decided to rename the vars and saw that I had mistyped the order of $fieldCount & $n... "Fixing" it creates a string with "Array....." in it.

    ----
    echo $i;
    outputs: Array,Array,Array.... as many as the original instances of arrays.

    print_r($i);
    outputs:

    string(72) "Array
    Array
    Array
    Array
    Array
    Array
    Array
    Array
    "

    ---- Since then I've been trying to understand it... only in the last few hours did I disover what was making it work. I just don't know why, or if I can continue to use the function safely.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •