...

View Full Version : Need help with outputting table SQL-PHP



ABM2007
01-30-2007, 12:47 PM
Hello,

I need the help of you masterminds to solve what seems to be a small issue, but goes out of my league.

I have the following script:


$data = mysql_query("SELECT * FROM temp_26_01_2007_22_00_25 WHERE $field LIKE'%$find%'");

$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

$oldarticle = "";

while($result = @mysql_fetch_array( $data ))
{

echo "<a href=".$result['url-big']."><img src=".$result['url-small']."></a>";
if ($oldarticle == $result['Article']) // If it is the same article
{
echo $result['Article:'];
// Do nothing
}
else // If it is a new article
{
echo "<a href=".$result['url-groot']."><img src=".$result['url-klein']."></a>";
echo $result['Article:'];
$oldarticle = $result['Article'];
}
echo " ";
echo "<br>";
echo $result['zoek1'];
echo "<br>";
echo $result['zoek3'];
echo "<br>";
}

It does this: I will read out the database, and check if a picture for a few products are the same. IF it finds two articles that share the same picture, it will only output the article and not another picture.

Now i have been struggling with this for a few day's now. I have a simple table in which i want to show each product and a nice list of articles next to a picture... but somehow i cannot get the table to show up neatly.


<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table1">
<tr>
<td>
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table2">
<tr>
<td width="26" height="21"></td>
<td width="155" height="21"></td>
<td height="21"></td>
<td width="18" height="21" valign="bottom"></td>
</tr>
<tr>
<td width="26"></td>
<td width="155" valign="top">
<p align="center">Main picture</td>
<td valign="top">
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table3">
<tr>
<td><font face="Verdana" size="1">Article 1</font></td>
<td width="83">info button</td>
</tr>
<tr>
<td><font face="Verdana" size="1">Article 2</font></td>
<td width="83">info button</td>
</tr>
</table>
</td>
<td width="18"></td>
</tr>
<tr>
<td width="26"></td>
<td width="155">
<p align="center"><b><font face="Verdana" size="1">category</font></b></td>
<td></td>
<td width="18"></td>
</tr>
</table>
</td>
</tr>
</table>

It will either show good, ( when there is only one product ) and it will messup when there are more products. So i think the above code needs an extra loop or whatever, but i cant seem to figure it out...

Who can?

Thank you in advance for any help you can provide.
xAnne

stewis
01-30-2007, 01:03 PM
Hi,

Although im not totaly sure what you mean i may have spotted a small mistake in your code change echo $result['Article:']; to echo $result['Article'];

If this still dont sort your problem could you give a little more detail on what you mean?

Stewart

ABM2007
01-30-2007, 01:37 PM
Hi,

Although im not totaly sure what you mean i may have spotted a small mistake in your code change echo $result['Article:']; to echo $result['Article'];

If this still dont sort your problem could you give a little more detail on what you mean?

Stewart

Hi, there is not a problem in the code.
The problem is that i want the url-klein to be output in a collumn to the left, and that all articles that share the same url-klein to be output in a collum to the right. Which isn't too difficult... but, the table has to finish correctly before starting a new while otherwise the whole output will mess up.

And that is the problem, i cannot build a table with this code somehow. Was hoping anyone had an idea, maybe the query isn't comples enough, i really don't know anymore :D

ABM2007
01-30-2007, 02:21 PM
Allow me to explain a bit further..

I have added two attachments, the one called current.jpg is how the data will be shown now... It will output each result in a different table. Even though the articles have the same picture... This is a working script already:

http://www.codingforums.com/attachment.php?attachmentid=5022&d=1170166615

With the code i posted it will put articles with the same url-klein next to one picture without outputting duplicated. Very neat :D

But currently, i cannot get the result i want to... the second attachment shows you the result i need ( modified with paint :) )

And that's the problem, i can't get it to be like the result.jpg picture:
http://www.codingforums.com/attachment.php?attachmentid=5023&d=1170166620

Hope this helps, and thank you for your help.

ABM2007
01-31-2007, 04:56 PM
I am starting to wonder if it is possible at all, been trying for a few day's now. :(

The insane thing is that i've seen it done so much, but where i spotted it they'are not willing to tell me. Grrrr

aedrin
01-31-2007, 07:49 PM
It is indeed possible. The trick is finding the best way to do it.

One way is to first parse out the results into an array according to the structure you want.

The way it would work is, first load your results from your database query.



$articles = array();
while($result = @mysql_fetch_array( $data )) {
$articles[] = $result;
}


So now you have a clean list of articles. (This list shouldn't consume a lot of memory since you should never have more than 30-50 articles on one page)

Next, you go through the list again and parse out the double articles.



$final_list = array(); // the final list to display
$tmp_list = array(); // this will be an article to display, but contains multiple rows, one for each article with the same picture
$last_picture = ''; // this way we see if the picture was the same as the last one
foreach ($articles as $article) {
if ($last_picture != $article['picture']) {
// the picture is different, add our current article to the final list and make a new article
$final_list[] = $tmp_list;
$tmp_list = array();
} else {
// still the same picture, just add it to our current article
$tmp_list[] = $article;
}
$last_picture = $article['picture'];
}


What you end up with is an array such as this:



Array
(
[0] => Array
(
[0] => Array
(
[name] => Schaar (linkshandig)
[price] => 15
)
[1] => Array
(
[name] => Schaar (rechtshandig)
[price] => 15
)
)
[1] => Array
(
[0] => Array
(
[name] => Mes
[price] => 5
)
)
[2] => Array
(
[0] => Array
(
[name] => Vork (metaal)
[price] => 15
)
[1] => Array
(
[name] => Vork (plastic)
[price] => 10
)
)
)


You should be able to go through this and print out the appropriate HTML for a table.

Whenever an array in the final list array has more than 1 element, you know that there are multiple names for this product.

ABM2007
01-31-2007, 10:02 PM
The code works perfectly :D

Now this may be a really, really stupid question... but how do i output it to screen now?

Don't beat me up if it's really stupid :(

ABM2007
02-01-2007, 08:22 AM
Hmmm got it to work somehow, but with each new picture it will build the array larger and with the same data.

for example
Picture1:
article1

Picture2:
article1
article2

etc. it does not however just output the articles with the same picture.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum