ORDER BY (Need to show choosen category)

Discussion in 'PHP' started by Huo_Yang, Feb 3, 2009.

  1. #1
    Hi.

    Need some help to change script to show posts from a choosen category. Currently it orders it by:

    $results = $db->query("SELECT `description`,`name`,`id`,`remote_image`,`image` FROM `{$SQL_Ext}_media` WHERE `published` = 1 ORDER BY `id` asc");
    PHP:
    which is every post on the site. I would need it to show the posts from (as example) category 22.

    Following the full php and another php-file from the script where it has category stuff in it. Hope someone can help me (willing to pay a few bucks if you can help me out.)

    here the php-code which has to be modified:
    <?php
    $cache_stats = cache_time('frontpage_listall',$cache_frontpage_listall);
    if(1 == $cache_stats)
    {   
         $db->connect();  
         $results = $db->query("SELECT `description`,`name`,`id`,`remote_image`,`image` FROM `{$SQL_Ext}_media` WHERE `published` = 1 ORDER BY `id` asc");
         while ($listall_row = $db->fetch_assoc($results))
         {
           
           //NAME
           if (strlen($listall_row['name']) > 50) 
            { $name = substr($listall_row['name'], 0, 50) . "..."; }
           else 
            { $name = $listall_row['name']; }
           
           $seoname = seo_str($listall_row['name']);
           if($seoname == '') { $seoname = 'unknown'; }       
           if ($seo_on == 0) 
            { $url = $site_url.'/index.php?task=view&id='.$listall_row['id']; }
           else 
            { $url = $site_url.'/'.$seotag.'/'.$listall_row['id']. '-'.$seoname.'.html'; }
    
           if ($listall_row['remote_image'] == 0) 
            { $icon = '<img src="'.$site_url.'/media/images/'.$listall_row['image'].'"  alt="" width="'.$image_width.'" height="'.$image_height.'" class="movieicon" />'; } 
           else 
            { $icon = '<img src="'.$listall_row['image'].'" alt="" width="'.$image_width.'" height="'.$image_height.'" class="movieicon" />'; }   
                           
    
             $frontpage_listall_str .= '
                   <td width="152" height="125" valign="top"><a href="'.$url.'">'.$icon.'</a><br><a href="'.$url.'" style="font-size:8px;" class="movie">'.$name.'</a></td>
             ';
            $x++;
            if($x == 5)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
            }              
         if($x == 10)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 15)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 20)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 25)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 30)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 35)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 40)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 45)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 50)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 55)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 if($x == 60)
            {
              $frontpage_listall_str .= '</tr>
      <tr>';
    	 }
    	 }
         unset($c);
         echo $frontpage_listall_str;
         cache_save('frontpage_listall',$frontpage_listall_str);
         unset($listall_row,$url,$icon,$name,$description,$seoname,$frontpage_listall_str,$frontpage_listall);     
         $db->close();
    }
    else
     { echo cache_read('frontpage_listall'); }           
    ?> 
    PHP:
    here the php-code where category stuff is in it for references:
    <?php
    
      defined('INSCRIPT') or die( '' );
    
     
      include_once ".{$template_url}/message.php";
      
          $db->connect();
          $result = $db->query("SELECT `id`,`name` FROM `{$SQL_Ext}_cats` WHERE `parentid` = '$catid' ORDER BY `name` ASC");
          if ($db->num_rows($result))
          {
              $x = 0;
              $content = "<table width='98%' border='0' cellpadding='3' cellspacing='4'>";          
              while ($row = $db->fetch_array($result))
              {
                  $x++;
                  $catid = $row['id'];
                  $catname = $row['name'];
                  $catname = stripslashes($catname);
                  $title = makesafe($catname);
                  
                  $result2 = $db->query("SELECT count(id) AS total_media from `{$SQL_Ext}_media` WHERE `published` = '1' && `catergory_id` = '$catid'");
                  $i = $db->fetch_assoc($result2);
                  $total_media = $i['total_media'];
                  
                  if ($total_media > 0)
                  {
                      if ($x == 1)
                       { $content = $content . "<tr>"; }
                      if ($seo_on == 0)
                       { $content .= '<td><a class="subCategoryList" title="' . $title . '" href="' . $site_url . '/index.php?cat_id=' . $row['id'] . '">' . $row['name'] . ' (' . $total_media . ')</a></td>'; }
                      else
                       { $content .= '<td><a class="subCategoryList" title="' . $title . '" href="' . $site_url . '/cat/' . $row['id'] . '.html">' . $row['name'] . ' (' . $total_media . ')</a></td>'; }
                      if ($x == 3)
                       { $content = $content . "</tr>"; $x = 0; }
                  }
              }
              unset($row,$catname,$title,$content);
              //end while
              if ($x > 0)
               { $content = $content . "</tr></table><br />"; }
              else
               { $content = $content . "</table><br />"; }
               
              echo '<div id="subcatitem"><table width="100%"  border="0" cellpadding="3" cellspacing="4" bgcolor="#FFFFFF"><tr> <td bgcolor="#2579CE" class="cellTitles"><a class="cellTitles" href="' . $site_url . '/index.php" style="text-decoration:none;">Home Page</a> >> Sub-Catagories</td></tr><tr> <td>' . $content . '</td></tr></table></div>';           
          }
          $db->close();
          
    
    
    
      $child_cat_ids = "";
      $db->connect();
      $result3 = $db->query("SELECT `id` FROM `{$SQL_Ext}_cats` WHERE `parentid` = '$catid';");
      if ($db->num_rows($result3))
       {
          while ($rows = $db->fetch_assoc($result3))
           {
              $child_cat_id = $rows['id'];
              $child_cat_ids .= "|| `catergory_id` = '$child_cat_id' ";
           }
          unset($rows);
       }
      $db->close();
      
      $db->connect();
      $total_results =$db->result($db->query("SELECT COUNT(*) as Num FROM `{$SQL_Ext}_media` WHERE published = '1' AND (`catergory_id` = $catid $child_cat_ids)"));
      $db->close();
      
      if ($total_results <= 0)
       { echo '<font size="3"><b>There are no videos in this area..</b></size>'; }   
      else
      {
          if(!isset($_GET['page']))  { $page = 1; } else  { $page = intval($_GET['page']); }
          
          $from = (($page * $max_results) - $max_results);
          
          $db->connect();
          $sql = $db->query("SELECT `name`,`description`,`id`,`remote_image`,`image` FROM `{$SQL_Ext}_media` WHERE `published` = '1'AND (`catergory_id` = $catid $child_cat_ids) ORDER BY `id` DESC LIMIT $from, $max_results;");
          echo '<table border="0" width="99%"><tr>';
          
          // gamenumber is used for table columns
          $num = "1";
          while ($row = $db->fetch_assoc($sql))
          {
              //name
              if (strlen($row['name']) > 50)
               { $name = substr($row['name'], 0, 50) . "..."; }
              else
               { $name = $row['name']; }
    
    
              //description
              if (strlen($row['description']) > 240)
               { $description = substr($row['description'], 0, 240) . "..."; }
              else
               { $description = $row['description']; }
              
              //video link gen
              $seoname = seo_str($row['name']);
              if ($seoname == '') { $seoname = 'unknown'; }
              if ($seo_on == 0) { $url = $site_url . '/index.php?task=view&id=' . $row['id']; }
              else { $url = $site_url . '/'.$seotag.'/' . $row['id'] . '-' . $seoname . '.html'; }
              
              //image link generate
              if ($row['remote_image'] == 0)
               { $img = '<img src="' . $site_url . '/media/images/' . $row['image'] . '" width="'.$image_width.'" height="'.$image_height.'" alt="" />'; }
              else
               { $img = '<img src="' . $row['image'] . '" width="'.$image_width.'" height="'.$image_height.'" alt="" />'; }
    
    
                    
              echo '<td width="50%" valign="top">
                   <table border="0" width="100%">
                   <tr>
                     <td width="100" height="150" rowspan="2" valign="top" align="left">
                       <a href="'.$url.'">'.$img.'</a>
                     </td>
                     <td valign="top" align="left">
                      <a href="'.$url.'" style="font-size:18px;" class="movie">'.$name.'</a>
                     </td>
                   </tr>
                   <tr>
                     <td valign="top" align="left" style="font-size:14px;">
                       '.$description.'
                     </td>
                   </tr>
                   </table>
                   <hr />
                   </td>';
              if ($num == '2') { echo "</tr><tr>"; $num = "0"; }
              $num++;
          }
          unset($url,$img,$num,$description,$row,$name,$seoname);
          echo '</tr></table>';
          $db->close();
    
          
        /** PAGES **/      
          $db->connect();    
          $total_results = $db->result($db->query("SELECT COUNT(*) as Num FROM `{$SQL_Ext}_media` WHERE `catergory_id` = '".$catid."' AND `published` = 1"));
          $db->close();
          
          $total_pages = ceil($total_results / $max_results);
          echo "<center>
                <br>Select a Page
                <br>";
                  if ($page > 1) { $prev = ($page - 1); if ($seo_on == 0) { echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$prev&cat_id=" . $catid . "\">&lt;&lt;Previous</a> "; } else { echo '<a href="' . $site_url . '/cat/' . $catid . '-p' . $prev . '.html">&lt;&lt;Previous</a> '; } }
                  for ($i = 1; $i <= $total_pages; $i++) { if (($page) == $i) { echo "<b><a href=#>$i</a></b> "; } else { if ($seo_on == 0) { echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$i&cat_id=" . $catid . "\">$i</a> "; } else { echo '<a href="' . $site_url . '/cat/' . $catid . '-p' . $i . '.html">' . $i . '</a> '; } } }
                  if ($page < $total_pages) { $next = ($page + 1); if ($seo_on == 0) { echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$next&cat_id=" . $catid . "\">Next>></a>"; } else { echo '<a href="' . $site_url . '/cat/' . $catid . '-p' . $next . '.html">Next>></a> '; } }
        //** PAGES **//      
      }
      
      return;
      
    ?>
    
    PHP:
    thanks in advance! tell me if you need more info!
     
    Huo_Yang, Feb 3, 2009 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    If 22 is the category:
    
    SELECT `description`,`name`,`id`,`remote_image`,`image` FROM `{$SQL_Ext}_media` WHERE `published` = 1 AND category_id=22 ORDER BY `id` asc"
    
    Code (SQL):
    If you are writing this script or application, I will suggest you first learn the basics, else you are going to write a buggy, bloated, inefficient code.

    If you are getting the script written from someone, then prolly you hired the wrong person D:
     
    rohan_shenoy, Feb 3, 2009 IP
  3. Huo_Yang

    Huo_Yang Peon

    Messages:
    250
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks.

    but this is what I get then
     
    Huo_Yang, Feb 3, 2009 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I think you must contact the original author of the script. I can guide you, though I feel you will screw somewhere in the process! :D

    BTW look carefully, isn't there any feature that sllowes users to browse videos in a particular category!
     
    rohan_shenoy, Feb 3, 2009 IP