SQL query results again

dragon2309

P.I Dragon
Does anyone know how to get more than one query result to be echoed on just one table row, as in have 2 or 3 results put on one row then drop down and do the next 2 or 3 etc etc.....

specifically aimed at cromewell and mgoldb2....


dragon2039
 
i'm not sure what you want here, explain again in fresh words, i have a few ideas of what you need that are doable but lets be clear
 
explain what tables and rows you want and what you want your output to be...I'm not really sure if you're talking about an Oracle Server, SQL Server or SQL injection or what?
 
Right, lets start over..........


INFORMATION:

I have a MySQL Databse with records of jewellery items (my parents business not mine), fields are such like StockCode, Dexcription, Price, IMG, URL.


CURRENT SYSTEM:

What currently happens is that one record (i.e. 1 jewelery item) is called up and is printed/echoed onto 1 row of a table on a PHP page.

You can see this in action currently on any of the jewellery pages at the website www.simplytrue.co.uk (use the menu at the top of the pages)

Graphical representation of current system:
table9iv.jpg



THEORETICAL NEW SYSTEM:

What i want to happen in an ideal world is to have all the results split into different pages, but i tried this and it failed miserably, back to the first question again...... What i want to happen is that more than one record (i.e. 2 jewellery records) can be put on one row of the table.

Graphical representation of theoretical new system:
table28cx.jpg



I know this can get a bit confusin, basically i want two records to be on each row of the table.


dragon2309
 
Oh i see so its a php question not a sql prob.

This task seems trival, what is the code you are using now to echo out the results from your query.

PS I like the work you have done on the site, nice clean lines. Bit to much pink though!
 
Last edited:
PS I like the work you have done on the site, nice clean lines. Bit to much pink though!
Lol, thanks, yeh, the pink isnt my decision but it could be toned down a bit i suppose.

Right then, the current code is.....:

PHP:
<?php
     include "dbinfo.php";

     // connect to SQL database
     if (!($connection = @ mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)))
       showerror();

     // use the databse shown in dbinfo.php
     if (!mysql_select_db(DB_DATABASENAME, $connection))
       showerror();

     // create the SQL query
     $query = "SELECT * FROM ad_neck WHERE StockCount > 0 ORDER BY StockCode DESC";

     // execute the query
     if (!($result = @ mysql_query ($query, $connection)))
       showerror();

	 // echo results
	while ($row = @ mysql_fetch_array($result))
       echo "<tr><td>{$row["StockCode"]}</td>
       <td>{$row["Description"]}</td>
       <td>{$row["Price"]}</td>
       <td><p><a href='{$row["IMGURL"]}'><img src='{$row["IMG"]}'></img></p></td>
       <td><center><a href='{$row["URL"]}'>$row[URL]</a></center></td></tr>";

   ?>

Very simple as you can see.... Do the query, echo the results into one line of the table. What i want is more than one result on any one line of the table. Is it feesable??



Oh i see so its a php question not a sql prob.
Yeh, sorry if the title of the thread was misleading but it is still technically manipulating the SQL query results.


thanks, dragon2309
 
Dragon conceptually this is a simple task to do. Although php is not my thing so the code i give may not be the best. It could have been easier if i knew (or could be bothered to look up) the syntax for shifting of arrays.

The code looks more bulky than it need be due to all the extra comments i added, you may remove these if you wish. You will also note i put a few options in there are well

I havent checked it as i dont have access to my web server from here. so i may have missed a ; here or there

PHP:
<?php
     include "dbinfo.php";

     // connect to SQL database
     if (!($connection = @ mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)))
       showerror();

     // use the databse shown in dbinfo.php
     if (!mysql_select_db(DB_DATABASENAME, $connection))
       showerror();

     // create the SQL query
     $query = "SELECT * FROM ad_neck WHERE StockCount > 0 ORDER BY StockCode DESC";

     // execute the query
     if (!($result = @ mysql_query ($query, $connection)))
       showerror();

     // echo results
	//create a counter
	$i = 0;
	$table_rows = "";
    while ($row = @ mysql_fetch_array($result))
	if (!$i){
		//if we have no items in the table row (as indicated by i) then add one.

		//note how this row we are adingis not finished with the </tr> yet... 
		//we need to add another item to the row
		//in the next loop
		$table_rows = "<tr><td>{$row["StockCode"]}</td>
       		<td>{$row["Description"]}</td>
      		<td>{$row["Price"]}</td>
     		<td><p><a href='{$row["IMGURL"]}'><img src='{$row["IMG"]}'></img></p></td>
     		<td><center><a href='{$row["URL"]}'>$row[url]</a></center></td>";

		$i++;//increment the counter telling how many items we have in the last table row to 1
		}else{

		//if we have 1 item in the row already then add another (using .=) and finish the row with </tr>
		$table_rows .= "<td>{$row["StockCode"]}</td>
       		<td>{$row["Description"]}</td>
      		<td>{$row["Price"]}</td>
     		<td><p><a href='{$row["IMGURL"]}'><img src='{$row["IMG"]}'></img></p></td>
     		<td><center><a href='{$row["URL"]}'>$row[url]</a></center></td></tr>";
		$i = 0;//reset the counter since we have just put the 2nd item in the row and next loop we
			//want to start an new row

		//echo $table_rows;//uncomment this if you choose to echo from within the loop (see noted below)
		}
	endwhile; //i use end while as i find it easier on the eye... even though its not efficent code

//now we are out of the loop we need to catch one error straight away. If the loop finished and there was only one item 
//added to the last row. We can check the $i counter to see if it is =1, if it is then we should really pad out the 
//rest of the final row with non breaking spaces and end the row with </.tr>
if ($i)
$table_rows .= "<td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td></tr>";

//at this point you have the whole table stored in the variable $tables_rows I noted that your code here did not include
//the tables header <Table> so i assume you have created the table above this php script. If you have you will need
//to edit this to include coloumn titles for the new columns. You will also note we havn't echoed anything out yet, to save
//operations (not that it really maters on this script) I echo to whole table now. If you disagree you will note that I
//have left an echo (which i commented out) in the above while loop; you can uncomment this echo in the loop and comment
//comment out the echo below. You may need to do this if the string $table_rows is getting huge!
echo $table_rows;
		

   ?>
 
PHP:
        //echo $table_rows;//uncomment this if you choose to echo from within the loop (see noted below)
        }
    endwhile; //i use end while as i find it easier on the eye... even though its not efficent code

//now we are out of the loop we need to catch one error straight away. If the loop finished and there was only one item

On this part when i go to execute the PHP page i get the following errorm i had a quick look back at it but i have never encountered "endwhile" so im not too sure what should be done with it

ERROR:
Parse error: parse error, unexpected T_ENDWHILE in /home/simplytrue/public_html/stocklist/test2.php on line 134

dragon2309
 
Sorry needed to add a ":" to the end of the while loop

while ($row = @ mysql_fetch_array($result)):

PS endwhile is a personal choice you can enclose the whole while loop in {} if you like.
 
Hey apj, thanks for all your work, the PHP block does execute now, but it will only ever echo 1 result in the table. Ive double checked the query sysntax to see if anything went amiss there, the only thing i can think of is that the loop isnt looping and its getting stpped after one passthrough.....

Following an example i was given on devshed i got it to work properly, so its no longer crucial that you help me, your example is currently at http://www.simplytrue.co.uk/stocklist/test5.php, the working one is at http://www.simplytrue.co.uk/stocklist/test2.php

I havent implemented it into the actual site yet, ive got a lot of exams to do in the next 2 weeks.

Thanks, dragon2039
 
hehe (talking to myself) thats what you get when you dont test the code yourself, you miss colons and in this case concatenation periods

This line should read.
if (!$i){
$table_rows .= "<tr><td>{$row["StockCode"]}</td>
 
Thanks apj, your code works now, although im not using it because i customised this other version so that it puts a colum gap between the two columns of results which is kinda cool...

But you are a PHP person in my head now, be prepared for future questions, cause i dont have the knowledge to implement by myself. I have the theory but not the knowledge.......

dragon2309
 
Back
Top