Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Small PHP MySQL problem,please help

Codes here !

Moderators: macek, egami, gesf

Small PHP MySQL problem,please help

Postby Novica89 » Fri May 18, 2012 5:42 am

I have a situation here. Other than my mental problems :)
I have a database from which i want to retrieve an <UL> of categories which correspond to the DISTINCT unique category names. So,for each differently named category_name field in my DB i want to create an <UL> with a <h2> title corresponding to the category name.Then,i want to create 5 <LI> elements inside of each of the created UL that are put under the same category_name field.
Everything works well with creating UL untill i put in my code to create <LI> elements inside those UL.If i put in the code for creating LI,then the whole code creates only ONE UL with 5 corresponding <LI> elements of that UL.

My code is this:
Code: Select all
<?php
               
   $query1 = "SELECT DISTINCT `category_name` FROM `links`";
      if ($query_run = mysql_query($query1)) {
         while($row = mysql_fetch_assoc($query_run)) {
            $title = $row['category_name'];
                                          
            echo "<div class=\"category\">
                  <h2>$title</h2>
                   <ul>";
                              
                  $query2 = "SELECT `image`,`link_description`,`link` FROM `links` WHERE `category_name`='$title' LIMIT 5";      
                  if ($query_run = mysql_query($query2)) {
                     while($row = mysql_fetch_array($query_run)) {
                        $tutorial_image = $row['image'];
                        $tutorial_desc = $row['link_description'];
                        $tutorial_link = $row['link'];
                        echo "<li><a href=\"$tutorial_link\" target=\"_blank\"><img src=\"images/$tutorial_image.png\" alt=\"$tutorial_image tutorial\"/>$tutorial_desc</a></li>";
                     }
                  }
               echo "</ul>
                  <div class=\"view_all\">
                     <div class=\"view_all_inner\">
                        <img src=\"images/doubleArrow.png\" alt=\">>\"/>
                        <a href=\"#\">view all</a>
                        <div class=\"clear\"></div>
                     </div>
                  </div>
                 </div>";
         }
      }
               
?>


Can anyone see where the problem with the code logic is? I would be grateful if you can help me :)
Novica89
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri May 18, 2012 5:34 am

Re: Small PHP MySQL problem,please help

Postby egami » Fri May 18, 2012 6:29 am

$query = "SELECT DISTINCT(category_name) FROM table";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$category = $row['category_name'];
echo '<h1>'.$category.'</h1>';
echo '<ul>';
$q = "SELECT * FROM other_table WHERE category_name = '$category'";
$r = mysql_query($q);
if (mysql_num_rows($r) > 0) {
while ($x = mysql_fetch_assoc($r)) {
echo '<li>'.$row['field_name'].'</li>';
}
} else {
echo '<li>Can\'t Find What You\'re Looking For...</li>';
}
echo '
</ul>
';
}


Something like that?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Small PHP MySQL problem,please help

Postby Novica89 » Fri May 18, 2012 7:32 am

Thing is,I'm going for just one table,not more. Can it be done this way? My DB looks like this
Attachments
db.png
db.png (32.56 KiB) Viewed 370 times
Novica89
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri May 18, 2012 5:34 am

Re: Small PHP MySQL problem,please help

Postby egami » Fri May 18, 2012 9:39 am

You'll have to build multiple queries.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Small PHP MySQL problem,please help

Postby Novica89 » Sat May 19, 2012 2:41 am

Thanks man ! :) I made two tables just like in your code and made relations between them,now it all works! :) Thanks a bunch :)
Novica89
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri May 18, 2012 5:34 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.