473,561 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a for loop within SELECT statement?

36 New Member
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 10128
pbmods
5,821 Recognized Expert Expert
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
micky125
36 New Member
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 Recognized Expert Expert
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
micky125
36 New Member
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
14763
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 misc endings. So I will require the search string Blue.*
3
3097
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 = " + "'" + programText.Text + "'"; I want to search the text box to see if it contains % and if it does I want to use like in the select statement above instead of =. It...
5
7095
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 connection of an open Asyncore socket. Calling the socket.close() nor the socket.shutdown(2) calls seem to work. The only way I can close the...
3
3164
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". Here is what I have so far: Dim arrCall As Variant ReDim arrCall(2) arrCall(0) = "GetData1" arrCall(1) = "GetData2"
5
3750
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 value="P01">P01 </option> <option value="P02">P02 </option> <option value="P03">P03 </option> <option...
2
6005
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) in ('01','02','05')) then 1 when (SUBSTR(field,1,2) in ('15','16')) then 2 when (SUBSTR(field,1,2) in ('18','19','22','36') or SUBSTR(field,1,3) in...
3
1715
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 first select is project and i am asking user to select it by using <?php $sql = "SELECT ProjectName FROM ...
10
8460
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 template i have the following code: <?php $images = $_POST; switch ($images) { case 1: echo '<img...
2
4818
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
7558
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8072
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7924
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5177
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3591
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2055
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 we have to send another system
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
884
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.