Complicated while loop for a simpleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nancy

    Complicated while loop for a simpleton

    Greetings:

    First, I apologize if my posting format is improper. The code below does
    what I intended it to do, but presently only displays 1 table entry. I've
    grown it to this point, but really need it to loop through the table and do
    everything where data_store_no matches $store_no. I've tried placing where
    at a couple different points with no real success - it either doesn't work
    at all, exceeds the time allowed for a process (presently set to 60 sec) or
    still only displays one table row. I regularly have problems understanding
    the online documentation and am obviously way over my head. Am I trying to
    do too much for it to loop? I need to display, in table format, 4 fields
    from each row that appears in the table where it matches store_no. I am
    limited to php ver 4.3.2. Any efficiency pointers and stupid mistake
    notices are also welcome.
    TIA!!
    nan

    Code:
    $store_no = $HTTP_GET_VARS['store_no'];
    
    // Lets look and see if there are any counts on file to retrieve
    $data_count_query = db_query("select data_query_no, data_query_id,
    data_store_no, data_count, data_return_code, data_return_message,
    data_select, data_query_date, data_zip4, data_radius from data_count_request
    where data_store_no = '" . $store_no . "'");
    // Set some variables we'll need
    $zip4 = $data_count_values['data_zip4'];
    $data_date = $data_count_values['data_query_date'];
    $data_date_formatted = substr($data_date,4,2) . '/' . substr($data_date,6,2)
    .. '/' . substr($data_date,0,4);
    
    if ($data_count_values['data_store_no']) { // There is something in the
    data_count_request table for this store
    // set titles for data selects
    $DS001 = 'Nurturing Moms';
    $DS002 = 'Fill';
    $DS003 = 'Leading Edge Food Focus';
    $data_select = $$data_count_values['data_select'];  // turns returned
    field into acutal variable
    // Check and see what the status of the count is
    if ($data_count_values['data_return_code']  == '99' AND
    $data_count_values['data_count'] == '0') {
    // This count was still processing earlier, lets look again and see
    if it's ready
    // Specify the URL string to open
    $queryid = $data_count_values['data_query_id'];
    $url =
    "http://data.website.com/databridge/databridge.asp?id=12345&pwd=PaSsWoRd&actioncode=2&campaign=01&queryid=$queryid";
    // open the url, get the returned data, close the url.
    $fp = fopen($url,  "r");
    $strYourXML = fgets($fp);
    fclose($fp);
    
    // Parse the returned XML into a multidemensional array
    $objXML = new xml2Array();
    $arrOutput = $objXML->parse($strYourXML);
    
    // Flatten the array into a single array
    flattenArray($arrOutput);
    // Format it in a way we can use
    $formatted = array(
    $tmp['name0']=>$tmp['tagData0'], // COUNT
    $tmp['name1']=>$tmp['tagData1'], // PRICE
    $tmp['name2']=>$tmp['tagData2'], // QUERYID
    $tmp['name3']=>$tmp['tagData3'], // CAMPAIGN
    $tmp['name4']=>$tmp['tagData4'], // RETURNCODE
    $tmp['name5']=>$tmp['tagData5'], // RETURNMSG
    );
    if ($formatted['RETURNCODE'] == '0' AND
    $formatted['RETURNMSG'] == 'success') {
    $count = $formatted['COUNT']; // count returned from
    Experian
    db_query("update data_count_request set  data_count =
    '" . $count . "', data_return_code = '" . $formatted['RETURNCODE'] . "',
    data_return_message = '" . $formatted['RETURNMSG'] . "' where data_query_id
    = '" . $formatted['QUERYID'] . "'");
    }
    // Checked data web site and count still processing
    elseif ($formatted['RETURNCODE'] == '99' AND
    $formatted['COUNT'] == '0'){ // count still processing
    
    $count = '<font color="red">Count Still Processing</font>';
    }
    }
    // Pull counts from table if they are there
    if ($data_count_values['data_return_code']  == '0' AND
    $data_count_values['data_return_message'] == 'success') {
    $count = $data_count_values['data_count'];
    
    $data_listing = '<tr><td>' .  $data_select . '</td>
    <td>' . $count . '</td>
    <td>' . $data_count_values['data_radius'] . '
    mi.</td>
    <td>' . $data_date_formatted .'</td></tr>';
    }
    } else { // There are no count requests for this store.
    $count = '<font color="red">No requests have been made</font>';
    }

  • David Haynes

    #2
    Re: Complicated while loop for a simpleton

    Nancy wrote:[color=blue]
    > Greetings:
    >
    > First, I apologize if my posting format is improper. The code below does
    > what I intended it to do, but presently only displays 1 table entry. I've
    > grown it to this point, but really need it to loop through the table and do
    > everything where data_store_no matches $store_no. I've tried placing where
    > at a couple different points with no real success - it either doesn't work
    > at all, exceeds the time allowed for a process (presently set to 60 sec) or
    > still only displays one table row. I regularly have problems understanding
    > the online documentation and am obviously way over my head. Am I trying to
    > do too much for it to loop? I need to display, in table format, 4 fields
    > from each row that appears in the table where it matches store_no. I am
    > limited to php ver 4.3.2. Any efficiency pointers and stupid mistake
    > notices are also welcome.
    > TIA!!
    > nan
    >
    >
    Code:
    > $store_no = $HTTP_GET_VARS['store_no'];
    >
    > // Lets look and see if there are any counts on file to retrieve
    > $data_count_query = db_query("select data_query_no, data_query_id,
    > data_store_no, data_count, data_return_code, data_return_message,
    > data_select, data_query_date, data_zip4, data_radius from data_count_request
    > where data_store_no = '" . $store_no . "'");
    > // Set some variables we'll need
    > $zip4 = $data_count_values['data_zip4'];
    > $data_date = $data_count_values['data_query_date'];
    > $data_date_formatted = substr($data_date,4,2) . '/' . substr($data_date,6,2)
    > . '/' . substr($data_date,0,4);
    >
    > if ($data_count_values['data_store_no']) { // There is something in the
    > data_count_request table for this store
    >     // set titles for data selects
    >         $DS001 = 'Nurturing Moms';
    >         $DS002 = 'Fill';
    >         $DS003 = 'Leading Edge Food Focus';
    >     $data_select = $$data_count_values['data_select'];  // turns returned
    > field into acutal variable
    >   // Check and see what the status of the count is
    >      if ($data_count_values['data_return_code']  == '99' AND
    > $data_count_values['data_count'] == '0') {
    >         // This count was still processing earlier, lets look again and see
    > if it's ready
    >             // Specify the URL string to open
    >                 $queryid = $data_count_values['data_query_id'];
    >       $url =
    > "http://data.website.com/databridge/databridge.asp?id=12345&pwd=PaSsWoRd&actioncode=2&campaign=01&queryid=$queryid";
    >         // open the url, get the returned data, close the url.
    >          $fp = fopen($url,  "r");
    >          $strYourXML = fgets($fp);
    >          fclose($fp);
    >
    >      // Parse the returned XML into a multidemensional array
    >          $objXML = new xml2Array();
    >          $arrOutput = $objXML->parse($strYourXML);
    >
    >      // Flatten the array into a single array
    >   flattenArray($arrOutput);
    >      // Format it in a way we can use
    >         $formatted = array(
    >    $tmp['name0']=>$tmp['tagData0'], // COUNT
    >           $tmp['name1']=>$tmp['tagData1'], // PRICE
    >           $tmp['name2']=>$tmp['tagData2'], // QUERYID
    >           $tmp['name3']=>$tmp['tagData3'], // CAMPAIGN
    >           $tmp['name4']=>$tmp['tagData4'], // RETURNCODE
    >           $tmp['name5']=>$tmp['tagData5'], // RETURNMSG
    >    );
    >                         if ($formatted['RETURNCODE'] == '0' AND
    > $formatted['RETURNMSG'] == 'success') {
    >                        $count = $formatted['COUNT']; // count returned from
    > Experian
    >                        db_query("update data_count_request set  data_count =
    > '" . $count . "', data_return_code = '" . $formatted['RETURNCODE'] . "',
    > data_return_message = '" . $formatted['RETURNMSG'] . "' where data_query_id
    > = '" . $formatted['QUERYID'] . "'");
    >                        }
    >                       // Checked data web site and count still processing
    >                       elseif ($formatted['RETURNCODE'] == '99' AND
    > $formatted['COUNT'] == '0'){ // count still processing
    >
    >               $count = '<font color="red">Count Still Processing</font>';
    >           }
    >        }
    >          // Pull counts from table if they are there
    >          if ($data_count_values['data_return_code']  == '0' AND
    > $data_count_values['data_return_message'] == 'success') {
    >                $count = $data_count_values['data_count'];
    >
    >         $data_listing = '<tr><td>' .  $data_select . '</td>
    >                              <td>' . $count . '</td>
    >                              <td>' . $data_count_values['data_radius'] . '
    > mi.</td>
    >                              <td>' . $data_date_formatted .'</td></tr>';
    >         }
    >     } else { // There are no count requests for this store.
    >     $count = '<font color="red">No requests have been made</font>';
    >   }
    >
    >
    >[/color]

    One thing that occurs to me is that the line:
    $data_listing = '<tr><td>' . $data_select . '</td>[color=blue]
    > <td>' . $count . '</td>
    > <td>' .[/color]
    $data_count_val ues['data_radius'] . '[color=blue]
    > mi.</td>
    > <td>' . $data_date_form atted .'</td></tr>';[/color]

    over-writes any previous value of $data_listing.
    Is this what you intended?

    Suggestions:
    Why not make your queries more readable/maintainable?

    $sql = <<< SQL
    SELECT
    data_query_no,
    data_query_id,
    data_store_no,
    data_count,
    data_return_cod e,
    data_return_mes sage,
    data_select,
    data_query_date ,
    data_zip4,
    data_radius
    FROM data_count_requ est
    WHERE data_store_no = '$store_no'
    SQL;

    $data_count_que ry = db_query($sql);

    -david-

    Comment

    • Nancy

      #3
      Re: Complicated while loop for a simpleton


      "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
      news:yXrMf.3589 6$XG1.23443@fe8 1.usenetserver. com...[color=blue]
      > Nancy wrote:
      >
      > One thing that occurs to me is that the line:
      > $data_listing = '<tr><td>' . $data_select . '</td>[color=green]
      > > <td>' . $count . '</td>
      > > <td>' .[/color]
      > $data_count_val ues['data_radius'] . '[color=green]
      > > mi.</td>
      > > <td>' . $data_date_form atted .'</td></tr>';[/color]
      >
      > over-writes any previous value of $data_listing.
      > Is this what you intended?[/color]

      No. I want it to add to, not overwrite. Will $data_listing .= fix that?
      [color=blue]
      > Suggestions:
      > Why not make your queries more readable/maintainable?[/color]

      This is an excellent suggestion, thank you very much David.
      [color=blue]
      > $sql = <<< SQL
      > SELECT
      > data_query_no,
      > data_query_id,
      > data_store_no,
      > data_count,
      > data_return_cod e,
      > data_return_mes sage,
      > data_select,
      > data_query_date ,
      > data_zip4,
      > data_radius
      > FROM data_count_requ est
      > WHERE data_store_no = '$store_no'
      > SQL;
      >
      > $data_count_que ry = db_query($sql);
      >
      > -david-
      >[/color]


      Comment

      • Iván Sánchez Ortega

        #4
        Re: Complicated while loop for a simpleton

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Nancy wrote:
        [color=blue]
        > First, I apologize if my posting format is improper.[/color]

        That chunk of code makes my head spin. But apart from that, it's fine.
        [color=blue]
        > Any efficiency pointers and stupid mistake notices are also welcome.[/color]

        I cannot go through all that code, but I'll drop some suggestions:

        - - Use variable expansion in your strings. It makes the code more legible.
        e.g.:
        <?php
        db_query("selec t * from table where data_store_no = $store_no");
        ?>


        - - Your db_query function may use global variables to store the DB
        connection, and the query result. If this is the case, avoid nesting
        db_query calls under any cost!! You might as well store all data from the
        first query in an array, then loop through that array with foreach(), and
        make sub-queries inside that loop.
        e.g., the following doesn't work:
        <?php
        $r = mysql_query("se lect * from user where company_id = $company_id");
        while($row = mysql_fetch_row ($r))
        {
        $user_id =$row['id'];
        // The following will overwrite the value of $r and mess with the previous
        while loop:
        $r = mysql_query("se lect * from orders where user_id = $user_id");
        // (Do whatever with the data from the last query)
        }
        ?>
        Your code may be doing something like that behind the scenes.


        - - Simplify and *understand* your code. If you don't know what does this
        piece of code do, or why this bit is here, you're on the wrong track.
        Declare functions and user docbook-style comments to know what the
        different chunks of code do. The code you posted looks a bit like
        gibberish.


        - - KISS. Keep simple things simple. Do you have any real reasons to use
        flattenArray or variable variables ($$data_count_v alues['data_select']) ??


        - --
        - ----------------------------------
        Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

        "Mostly harmless."
        -- The Hitchhiker's Guide to the Galaxy.
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.2 (GNU/Linux)

        iD8DBQFEAkiv3jc Q2mg3Pc8RAv+bAJ 9SmXFKapHNG/2mDLLS1MwjNu8K4 ACghkVy
        maI90arkXyInTPp aHZFpcPs=
        =VZ/z
        -----END PGP SIGNATURE-----

        Comment

        • David Haynes

          #5
          Re: Complicated while loop for a simpleton

          Nancy wrote:[color=blue]
          > "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
          > news:yXrMf.3589 6$XG1.23443@fe8 1.usenetserver. com...[color=green]
          >> Nancy wrote:
          >>
          >> One thing that occurs to me is that the line:
          >> $data_listing = '<tr><td>' . $data_select . '</td>[color=darkred]
          >>> <td>' . $count . '</td>
          >>> <td>' .[/color]
          >> $data_count_val ues['data_radius'] . '[color=darkred]
          >>> mi.</td>
          >>> <td>' . $data_date_form atted .'</td></tr>';[/color]
          >> over-writes any previous value of $data_listing.
          >> Is this what you intended?[/color]
          >
          > No. I want it to add to, not overwrite. Will $data_listing .= fix that?[/color]

          Well, I would do something like:

          $data_list .= <<<HTML
          <tr>
          <td>$data_selec t</td>
          <td>$count</td>
          <td>{$data_coun t_values['data_radius']} mi</td>
          <td>$data_date_ formatted</td>
          </tr>
          HTML;

          because I like to see the HTML structure as much as I can.

          -david-

          Comment

          • Nancy

            #6
            Re: Complicated while loop for a simpleton

            Ivan,

            Thank you very much for your insight. Some very good input. I admit that I
            do seriously lack understanding but am getting better.

            If I had a better understanding, I could probably do without flattenArray,
            but XML is returned from opening the url and I couldn't easily grasp how to
            extract what I needed from the XML (not running php 5, so simple_xml is not
            an option) so I used code from php.net to convert the XML into a
            multi-dimensional array, but still unable to grasp how to extract what I
            needed from a multi-dimensional array, I flatten into something I do
            understand how to deal with.

            Thanks again for your time!
            nan
            "Iván Sánchez Ortega" <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote in
            message news:s0u9d3-up.ln1@blackspa rk.escomposlinu x.org...[color=blue]
            > -----BEGIN PGP SIGNED MESSAGE-----
            > Hash: SHA1
            >
            > Nancy wrote:
            >[color=green]
            >> First, I apologize if my posting format is improper.[/color]
            >
            > That chunk of code makes my head spin. But apart from that, it's fine.
            >[color=green]
            >> Any efficiency pointers and stupid mistake notices are also welcome.[/color]
            >
            > I cannot go through all that code, but I'll drop some suggestions:
            >
            > - - Use variable expansion in your strings. It makes the code more
            > legible.
            > e.g.:
            > <?php
            > db_query("selec t * from table where data_store_no = $store_no");
            > ?>
            >
            >
            > - - Your db_query function may use global variables to store the DB
            > connection, and the query result. If this is the case, avoid nesting
            > db_query calls under any cost!! You might as well store all data from the
            > first query in an array, then loop through that array with foreach(), and
            > make sub-queries inside that loop.
            > e.g., the following doesn't work:
            > <?php
            > $r = mysql_query("se lect * from user where company_id = $company_id");
            > while($row = mysql_fetch_row ($r))
            > {
            > $user_id =$row['id'];
            > // The following will overwrite the value of $r and mess with the
            > previous
            > while loop:
            > $r = mysql_query("se lect * from orders where user_id = $user_id");
            > // (Do whatever with the data from the last query)
            > }
            > ?>
            > Your code may be doing something like that behind the scenes.
            >
            >
            > - - Simplify and *understand* your code. If you don't know what does this
            > piece of code do, or why this bit is here, you're on the wrong track.
            > Declare functions and user docbook-style comments to know what the
            > different chunks of code do. The code you posted looks a bit like
            > gibberish.
            >
            >
            > - - KISS. Keep simple things simple. Do you have any real reasons to use
            > flattenArray or variable variables ($$data_count_v alues['data_select']) ??
            >
            >
            > - --
            > - ----------------------------------
            > Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net
            >
            > "Mostly harmless."
            > -- The Hitchhiker's Guide to the Galaxy.
            > -----BEGIN PGP SIGNATURE-----
            > Version: GnuPG v1.4.2 (GNU/Linux)
            >
            > iD8DBQFEAkiv3jc Q2mg3Pc8RAv+bAJ 9SmXFKapHNG/2mDLLS1MwjNu8K4 ACghkVy
            > maI90arkXyInTPp aHZFpcPs=
            > =VZ/z
            > -----END PGP SIGNATURE-----[/color]


            Comment

            • Nancy

              #7
              Re: Complicated while loop for a simpleton


              "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
              news:GRsMf.5308 $Y35.2216@fe36. usenetserver.co m...
              [color=blue]
              > $data_list .= <<<HTML
              > <tr>
              > <td>$data_selec t</td>
              > <td>$count</td>
              > <td>{$data_coun t_values['data_radius']} mi</td>
              > <td>$data_date_ formatted</td>
              > </tr>
              > HTML;[/color]

              I didn't know it could be done like this. This would make things a lot
              better.
              [color=blue]
              > because I like to see the HTML structure as much as I can.
              >
              > -david-
              >[/color]


              Comment

              • Iván Sánchez Ortega

                #8
                Re: Complicated while loop for a simpleton

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                Nancy wrote:
                [color=blue]
                > If I had a better understanding, I could probably do without flattenArray,
                > but XML is returned from opening the url[/color]

                Wait, wait, wait. Are you saying that the code: ??
                - - Runs a SQL query,
                - - Then grabs a XML file from god-knows-where, based on the data of that
                query,
                - - Then runs another SQL query based on the data on the XML file.

                Who in the earth designed that? My head is spinning again...

                - --
                - ----------------------------------
                Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


                Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
                5.1.2-1 generating this signature.
                Uptime: 10:03:03 up 2 days, 13:11, 1 user, load average: 0.15, 0.51, 0.47

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.2 (GNU/Linux)

                iD8DBQFEAsDo3jc Q2mg3Pc8RAjGQAK CD4WHCtdbdlrvvi 9FUEXdHAPxmTQCf beAX
                N8YvbVOa/6k61JfXC7V3mbM=
                =hjdj
                -----END PGP SIGNATURE-----

                Comment

                • Nancy

                  #9
                  Re: Complicated while loop for a simpleton

                  ..= was the main problem, once it wasn't overwriting the output, it was
                  easier to see the remaining problems. Thanks to you and Dave!
                  nan

                  "Iván Sánchez Ortega" <i.punto.sanche z--@rroba--mirame.punto.ne t> wrote in
                  message news:q2sad3-l22.ln1@blacksp ark.escomposlin ux.org...[color=blue]
                  > -----BEGIN PGP SIGNED MESSAGE-----
                  > Hash: SHA1
                  >
                  > Nancy wrote:
                  >[color=green]
                  >> If I had a better understanding, I could probably do without
                  >> flattenArray,
                  >> but XML is returned from opening the url[/color]
                  >
                  > Wait, wait, wait. Are you saying that the code: ??
                  > - - Runs a SQL query,
                  > - - Then grabs a XML file from god-knows-where, based on the data of that
                  > query,
                  > - - Then runs another SQL query based on the data on the XML file.
                  >
                  > Who in the earth designed that? My head is spinning again...
                  >
                  > - --
                  > - ----------------------------------
                  > Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net
                  >
                  > http://acm.asoc.fi.upm.es/~mr/
                  > Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
                  > 5.1.2-1 generating this signature.
                  > Uptime: 10:03:03 up 2 days, 13:11, 1 user, load average: 0.15, 0.51,
                  > 0.47
                  >
                  > -----BEGIN PGP SIGNATURE-----
                  > Version: GnuPG v1.4.2 (GNU/Linux)
                  >
                  > iD8DBQFEAsDo3jc Q2mg3Pc8RAjGQAK CD4WHCtdbdlrvvi 9FUEXdHAPxmTQCf beAX
                  > N8YvbVOa/6k61JfXC7V3mbM=
                  > =hjdj
                  > -----END PGP SIGNATURE-----[/color]


                  Comment

                  Working...