Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with outputting table SQL-PHP

    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:

    PHP Code:
    $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.

    Code:
    <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

  • #2
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by stewis View Post
    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

  • #4
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:



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

    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:


    Hope this helps, and thank you for your help.
    Attached Thumbnails Attached Thumbnails Need help with outputting table SQL-PHP-current.jpg   Need help with outputting table SQL-PHP-result.jpg  
    Last edited by ABM2007; 01-30-2007 at 03:24 PM.

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

    PHP Code:
    $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.

    PHP Code:
    $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:

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

  • #7
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code works perfectly

    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

  • #8
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by ABM2007; 02-01-2007 at 11:04 AM. Reason: Because im naughty?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •