473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

using a for loop within SELECT statement?

Hi all, i've been pondering with an idea for my system to allow the user to make reports depending on what he needs. His input screen will be a series of select options corresponding to the table columns. There will be the same number of select statements on screen as there are tables / columns in my database.
e.g

Table 1 = id, date, age (etc.....)
Table 2 = make model (etc....)
-----
-----
Table N = etc

________________________
Table1
select1 select2 select3

Table2
select4 select5

etc.

so the user can add really any number of these say up to 30(although will never get a report with this amount but keeps options open).

The problem i can foresee is in using mysql ( SELECT (for($i=1, $i<=30, i++) etc. if i can get this working it will mean i would not have to hard code say even up to 10 queries.

As I am new to this programming in PHP and mySQL i was wondering if anyone could point me in the right direction as i feel php code cannot be used within sql code.

I am using PHP 5 with HTML.

Any help in this matter is much appreciated as it is the last thing i have to do so i can finish my project :)
Sep 15 '08 #1
4 10117
pbmods
5,821 Expert 4TB
Heya, Micky.

A query is just a string that PHP passes to the SQL server. You can modify it however you want before you pass it off to your query() method.

E.g.,:

Expand|Select|Wrap|Line Numbers
  1. for( $i = 0; $i < 99; ++$i )
  2. {
  3.   $res = $db->query("SELECT `stuff` FROM `table` WHERE `id` = '{$i}' LIMIT 1");
  4.  
  5.   // Do stuff with $res
  6. }
  7.  
Alternatively, you might be able to use a join in your SQL query and do away with all the extra query calls altogether. This would be a topic for the MySQL forum.
Sep 15 '08 #2
thanks for the quick reply pbmods
so will ur code give me the same as:

table 1 ----- 1 select option
table 2 ----- 3 select options

mysql( SELECT option1 option2 option3 option4 option5
FROM table1, table2
WHERE (the user will input two values here that will do this)
so that i can then make a report that will read

option1 option2 option3 option4 option5
record1
record2
-----
-----
recordN.

I think u have probably solved it i just cant seem to get my head around the code :(
Sep 15 '08 #3
pbmods
5,821 Expert 4TB
It sounds like you want to be able to report on items in one table that match criteria in a second table. Is that correct?

For example:

Expand|Select|Wrap|Line Numbers
  1. /** Open the HTML table. */
  2. echo '
  3. <table>
  4. ';
  5.  
  6. /** Run a query to fetch all options that the User selected. */
  7. $selectedItems =
  8.   $db->query
  9.   (
  10.     "
  11. SELECT
  12.          `ItemID`
  13.        , `Name`
  14.   FROM
  15.        `Items`
  16.  WHERE
  17.        `Description` LIKE '%{$searchTerm}%'
  18.     "
  19.   );
  20.  
  21. /** For each item in the result... */
  22. foreach( $selectedItems as $rowItem )
  23. {
  24.   /** ... output a header row for the item... */
  25.   echo "
  26. <tr>
  27.   <th colspan=\"3\">{$rowItem->Name}</th>
  28. </tr>";
  29.  
  30.   /** ... then run a separate query to fetch the options for that item. */
  31.   $itemOptions =
  32.     $db->query
  33.     (
  34.       "
  35. SELECT
  36.             `Options`.`OptionID`
  37.           , `Options`.`Name`
  38.           , `Options`.`Adjustment`
  39.   FROM
  40.               `Map_ItemOptions`
  41.     LEFT JOIN `Options`
  42.           USING (`OptionID`)
  43.  WHERE
  44.           `Map_ItemOptions`.`ItemID` = '{$row->ItemID}'
  45.  ORDER BY
  46.           `Options`.`Name` ASC
  47.       "
  48.     );
  49.  
  50.     /** For each option that we were able to fetch for the item... */
  51.     foreach( $itemOptions as $rowOptions )
  52.     {
  53.       /** ... output the information we fetched for that option. */
  54.       echo "
  55. <tr>
  56.   <td>{$rowOptions->OptionID}</td>
  57.   <td>{$rowOptions->Name}</td>
  58.   <td>{$rowOptions->Adjustment}</td>
  59. </tr>";
  60.     }
  61. }
  62.  
  63. /** Close our HTML table. */
  64. echo '
  65. </table>
  66. ';
  67.  
Sep 17 '08 #4
yeh thats spot on so it is. im going to implement now. Cheers for the help!
Sep 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David | last post by:
Hi, Quick question. If I have a recordset value in RS("ProductName"), is it possible to test for a part string in this value ? I would be looking for all products starting 'Blue' and with...
3
by: kscdavefl | last post by:
I am using the following select statement in a web app where I take the cvalue in a text box and conduct a search of the database. "Select wo18 from workorder where wo9 = " + "'" +...
5
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the...
3
by: nico3334 | last post by:
Hi, I have an IF..Then statement that I am using to determine which procedure to Call. Within the "else" section, I want to use a For..Next coding to loop through 3 different "call procedures". ...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
2
by: carmelo | last post by:
Hello, I'm working on a DB on which I can run only SELECT commands, so I'd like to modify this UPDATE command, which makes use of CASE: update Table set field2 = case when (SUBSTR(field,1,2)...
3
by: micky125 | last post by:
Hey guys I have been checking the forum for a way to populate a second select field depending on the choice made from the first one. Basically i am working on projects and the delays that occur. The...
10
by: stewdizzle | last post by:
I am setting up a small site for personal use. It consists of a form that transfers inputed values to an html template. The ouput is code that i can use to quickly post on a website. In the...
2
by: ankitmathur | last post by:
Hi All, I'm facing a problem I'm unable to find a solution for. I hope fellow members would be able to help me out. System Info: PHP v5 MSSQL 2008 Linux box
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.