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.

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

_______________ _________
select1 select2 select3

select4 select5


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
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.


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");
  5.   // Do stuff with $res
  6. }
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
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

I think u have probably solved it i just cant seem to get my head around the code :(
Sep 15 '08 #3
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. ';
  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.   );
  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>";
  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.     );
  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. }
  63. /** Close our HTML table. */
  64. echo '
  65. </table>
  66. ';
Sep 17 '08 #4
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

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.*
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...
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...
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"
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...
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...
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 ...
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...
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
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...
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. ...
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...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.