473,466 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Expand|Select|Wrap|Line Numbers
  1. $store_no = $HTTP_GET_VARS['store_no'];
  2.  
  3. // Lets look and see if there are any counts on file to retrieve
  4. $data_count_query = db_query("select data_query_no, data_query_id,
  5. data_store_no, data_count, data_return_code, data_return_message,
  6. data_select, data_query_date, data_zip4, data_radius from data_count_request
  7. where data_store_no = '" . $store_no . "'");
  8. // Set some variables we'll need
  9. $zip4 = $data_count_values['data_zip4'];
  10. $data_date = $data_count_values['data_query_date'];
  11. $data_date_formatted = substr($data_date,4,2) . '/' . substr($data_date,6,2)
  12. .. '/' . substr($data_date,0,4);
  13.  
  14. if ($data_count_values['data_store_no']) { // There is something in the
  15. data_count_request table for this store
  16. // set titles for data selects
  17. $DS001 = 'Nurturing Moms';
  18. $DS002 = 'Fill';
  19. $DS003 = 'Leading Edge Food Focus';
  20. $data_select = $$data_count_values['data_select'];  // turns returned
  21. field into acutal variable
  22. // Check and see what the status of the count is
  23. if ($data_count_values['data_return_code']  == '99' AND
  24. $data_count_values['data_count'] == '0') {
  25. // This count was still processing earlier, lets look again and see
  26. if it's ready
  27. // Specify the URL string to open
  28. $queryid = $data_count_values['data_query_id'];
  29. $url =
  30. "http://data.website.com/databridge/databridge.asp?id=12345&pwd=PaSsWoRd&actioncode=2&campaign=01&queryid=$queryid";
  31. // open the url, get the returned data, close the url.
  32. $fp = fopen($url,  "r");
  33. $strYourXML = fgets($fp);
  34. fclose($fp);
  35.  
  36. // Parse the returned XML into a multidemensional array
  37. $objXML = new xml2Array();
  38. $arrOutput = $objXML->parse($strYourXML);
  39.  
  40. // Flatten the array into a single array
  41. flattenArray($arrOutput);
  42. // Format it in a way we can use
  43. $formatted = array(
  44. $tmp['name0']=>$tmp['tagData0'], // COUNT
  45. $tmp['name1']=>$tmp['tagData1'], // PRICE
  46. $tmp['name2']=>$tmp['tagData2'], // QUERYID
  47. $tmp['name3']=>$tmp['tagData3'], // CAMPAIGN
  48. $tmp['name4']=>$tmp['tagData4'], // RETURNCODE
  49. $tmp['name5']=>$tmp['tagData5'], // RETURNMSG
  50. );
  51. if ($formatted['RETURNCODE'] == '0' AND
  52. $formatted['RETURNMSG'] == 'success') {
  53. $count = $formatted['COUNT']; // count returned from
  54. Experian
  55. db_query("update data_count_request set  data_count =
  56. '" . $count . "', data_return_code = '" . $formatted['RETURNCODE'] . "',
  57. data_return_message = '" . $formatted['RETURNMSG'] . "' where data_query_id
  58. = '" . $formatted['QUERYID'] . "'");
  59. }
  60. // Checked data web site and count still processing
  61. elseif ($formatted['RETURNCODE'] == '99' AND
  62. $formatted['COUNT'] == '0'){ // count still processing
  63.  
  64. $count = '<font color="red">Count Still Processing</font>';
  65. }
  66. }
  67. // Pull counts from table if they are there
  68. if ($data_count_values['data_return_code']  == '0' AND
  69. $data_count_values['data_return_message'] == 'success') {
  70. $count = $data_count_values['data_count'];
  71.  
  72. $data_listing = '<tr><td>' .  $data_select . '</td>
  73. <td>' . $count . '</td>
  74. <td>' . $data_count_values['data_radius'] . '
  75. mi.</td>
  76. <td>' . $data_date_formatted .'</td></tr>';
  77. }
  78. } else { // There are no count requests for this store.
  79. $count = '<font color="red">No requests have been made</font>';
  80. }
  81.  

Feb 26 '06 #1
8 1941
Nancy wrote:
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

Expand|Select|Wrap|Line Numbers
  1.  $store_no = $HTTP_GET_VARS['store_no'];
  2.  // Lets look and see if there are any counts on file to retrieve
  3.  $data_count_query = db_query("select data_query_no, data_query_id,
  4.  data_store_no, data_count, data_return_code, data_return_message,
  5.  data_select, data_query_date, data_zip4, data_radius from data_count_request
  6.  where data_store_no = '" . $store_no . "'");
  7.  // Set some variables we'll need
  8.  $zip4 = $data_count_values['data_zip4'];
  9.  $data_date = $data_count_values['data_query_date'];
  10.  $data_date_formatted = substr($data_date,4,2) . '/' . substr($data_date,6,2)
  11.  . '/' . substr($data_date,0,4);
  12.  if ($data_count_values['data_store_no']) { // There is something in the
  13.  data_count_request table for this store
  14.      // set titles for data selects
  15.          $DS001 = 'Nurturing Moms';
  16.          $DS002 = 'Fill';
  17.          $DS003 = 'Leading Edge Food Focus';
  18.      $data_select = $$data_count_values['data_select'];  // turns returned
  19.  field into acutal variable
  20.    // Check and see what the status of the count is
  21.       if ($data_count_values['data_return_code']  == '99' AND
  22.  $data_count_values['data_count'] == '0') {
  23.          // This count was still processing earlier, lets look again and see
  24.  if it's ready
  25.              // Specify the URL string to open
  26.                  $queryid = $data_count_values['data_query_id'];
  27.        $url =
  28.  "http://data.website.com/databridge/databridge.asp?id=12345&pwd=PaSsWoRd&actioncode=2&campaign=01&queryid=$queryid";
  29.          // open the url, get the returned data, close the url.
  30.           $fp = fopen($url,  "r");
  31.           $strYourXML = fgets($fp);
  32.           fclose($fp);
  33.       // Parse the returned XML into a multidemensional array
  34.           $objXML = new xml2Array();
  35.           $arrOutput = $objXML->parse($strYourXML);
  36.       // Flatten the array into a single array
  37.    flattenArray($arrOutput);
  38.       // Format it in a way we can use
  39.          $formatted = array(
  40.     $tmp['name0']=>$tmp['tagData0'], // COUNT
  41.            $tmp['name1']=>$tmp['tagData1'], // PRICE
  42.            $tmp['name2']=>$tmp['tagData2'], // QUERYID
  43.            $tmp['name3']=>$tmp['tagData3'], // CAMPAIGN
  44.            $tmp['name4']=>$tmp['tagData4'], // RETURNCODE
  45.            $tmp['name5']=>$tmp['tagData5'], // RETURNMSG
  46.     );
  47.                          if ($formatted['RETURNCODE'] == '0' AND
  48.  $formatted['RETURNMSG'] == 'success') {
  49.                         $count = $formatted['COUNT']; // count returned from
  50.  Experian
  51.                         db_query("update data_count_request set  data_count =
  52.  '" . $count . "', data_return_code = '" . $formatted['RETURNCODE'] . "',
  53.  data_return_message = '" . $formatted['RETURNMSG'] . "' where data_query_id
  54.  = '" . $formatted['QUERYID'] . "'");
  55.                         }
  56.                        // Checked data web site and count still processing
  57.                        elseif ($formatted['RETURNCODE'] == '99' AND
  58.  $formatted['COUNT'] == '0'){ // count still processing
  59.                $count = '<font color="red">Count Still Processing</font>';
  60.            }
  61.         }
  62.           // Pull counts from table if they are there
  63.           if ($data_count_values['data_return_code']  == '0' AND
  64.  $data_count_values['data_return_message'] == 'success') {
  65.                 $count = $data_count_values['data_count'];
  66.          $data_listing = '<tr><td>' .  $data_select . '</td>
  67.                               <td>' . $count . '</td>
  68.                               <td>' . $data_count_values['data_radius'] . '
  69.  mi.</td>
  70.                               <td>' . $data_date_formatted .'</td></tr>';
  71.          }
  72.      } else { // There are no count requests for this store.
  73.      $count = '<font color="red">No requests have been made</font>';
  74.    }
  75.  


One thing that occurs to me is that the line:
$data_listing = '<tr><td>' . $data_select . '</td> <td>' . $count . '</td>
<td>' . $data_count_values['data_radius'] . ' mi.</td>
<td>' . $data_date_formatted .'</td></tr>';


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_code,
data_return_message,
data_select,
data_query_date,
data_zip4,
data_radius
FROM data_count_request
WHERE data_store_no = '$store_no'
SQL;

$data_count_query = db_query($sql);

-david-

Feb 27 '06 #2

"David Haynes" <da***********@sympatico.ca> wrote in message
news:yX*******************@fe81.usenetserver.com.. .
Nancy wrote:

One thing that occurs to me is that the line:
$data_listing = '<tr><td>' . $data_select . '</td>
<td>' . $count . '</td>
<td>' . $data_count_values['data_radius'] . '
mi.</td>
<td>' . $data_date_formatted .'</td></tr>';


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


No. I want it to add to, not overwrite. Will $data_listing .= fix that?
Suggestions:
Why not make your queries more readable/maintainable?
This is an excellent suggestion, thank you very much David.
$sql = <<< SQL
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'
SQL;

$data_count_query = db_query($sql);

-david-

Feb 27 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nancy wrote:
First, I apologize if my posting format is improper.
That chunk of code makes my head spin. But apart from that, it's fine.
Any efficiency pointers and stupid mistake notices are also welcome.


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("select * 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("select * 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("select * 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_values['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)

iD8DBQFEAkiv3jcQ2mg3Pc8RAv+bAJ9SmXFKapHNG/2mDLLS1MwjNu8K4ACghkVy
maI90arkXyInTPpaHZFpcPs=
=VZ/z
-----END PGP SIGNATURE-----
Feb 27 '06 #4
Nancy wrote:
"David Haynes" <da***********@sympatico.ca> wrote in message
news:yX*******************@fe81.usenetserver.com.. .
Nancy wrote:

One thing that occurs to me is that the line:
$data_listing = '<tr><td>' . $data_select . '</td>
<td>' . $count . '</td>
<td>' .

$data_count_values['data_radius'] . '
mi.</td>
<td>' . $data_date_formatted .'</td></tr>';

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


No. I want it to add to, not overwrite. Will $data_listing .= fix that?


Well, I would do something like:

$data_list .= <<<HTML
<tr>
<td>$data_select</td>
<td>$count</td>
<td>{$data_count_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-

Feb 27 '06 #5
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.***************@rroba--mirame.punto.net> wrote in
message news:s0***********@blackspark.escomposlinux.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nancy wrote:
First, I apologize if my posting format is improper.


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


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("select * 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("select * 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("select * 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_values['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)

iD8DBQFEAkiv3jcQ2mg3Pc8RAv+bAJ9SmXFKapHNG/2mDLLS1MwjNu8K4ACghkVy
maI90arkXyInTPpaHZFpcPs=
=VZ/z
-----END PGP SIGNATURE-----

Feb 27 '06 #6

"David Haynes" <da***********@sympatico.ca> wrote in message
news:GR*****************@fe36.usenetserver.com...
$data_list .= <<<HTML
<tr>
<td>$data_select</td>
<td>$count</td>
<td>{$data_count_values['data_radius']} mi</td>
<td>$data_date_formatted</td>
</tr>
HTML;
I didn't know it could be done like this. This would make things a lot
better.
because I like to see the HTML structure as much as I can.

-david-

Feb 27 '06 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nancy wrote:
If I had a better understanding, I could probably do without flattenArray,
but XML is returned from opening the url


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)

iD8DBQFEAsDo3jcQ2mg3Pc8RAjGQAKCD4WHCtdbdlrvvi9FUEX dHAPxmTQCfbeAX
N8YvbVOa/6k61JfXC7V3mbM=
=hjdj
-----END PGP SIGNATURE-----
Feb 27 '06 #8
..= 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.***************@rroba--mirame.punto.net> wrote in
message news:q2************@blackspark.escomposlinux.org.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nancy wrote:
If I had a better understanding, I could probably do without
flattenArray,
but XML is returned from opening the url


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)

iD8DBQFEAsDo3jcQ2mg3Pc8RAjGQAKCD4WHCtdbdlrvvi9FUEX dHAPxmTQCfbeAX
N8YvbVOa/6k61JfXC7V3mbM=
=hjdj
-----END PGP SIGNATURE-----

Feb 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on...
15
by: Agoston Bejo | last post by:
Hi, I'm having trouble with implementing some constraints on the database level. An example: --Table A(AID, BID ref. B.BID, ATXT) --Table B(BID, CID ref. C.CID) --Table C(CID) upon insertion...
3
by: Chris | last post by:
Hello all- Fairly new to SQL and I need to issue a pretty complex query (complex being a relative term here :) ). To dumb down my example for display purposes, I have two tables in my schema...
7
by: windandwaves | last post by:
Hi Gurus I am trying to make this rather complicated maps with an a huge number of mouseovers and the like. I want to set up a function that OnMouseDown swaps the OnMouseOver and OnMouseOut for...
1
by: sathyashrayan | last post by:
Groups, Take a look at the following program taken from C snippet archive. -----------------------code------------------------ void bitstring(char *str, long byze, int biz, int strwid) { int...
26
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and...
1
by: viz | last post by:
hi, i am curious about simpleton classes. i want to know that is it sensible to derive from a simpleton class or should they be used directly. please help Thanx
1
by: Kev | last post by:
Hi, I am trying to total a column (Shift1) in a subform (continous forms) from the after update event of the (Shift1) column control within the subform. This column stores shift codes, I want to...
10
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.