Current location: Hot Scripts Forums » Programming Languages » PHP » One query for all?


One query for all?

Reply
  #1 (permalink)  
Old 07-07-10, 06:27 AM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 188
Thanks: 2
Thanked 0 Times in 0 Posts
One query for all?

I have one table and I need to read records of this table but with one query and where I want to recod number one just I put one function.
(i think I need one class)
Reply With Quote
  #2 (permalink)  
Old 07-07-10, 07:56 AM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 188
Thanks: 2
Thanked 0 Times in 0 Posts
PHP Code:

    public function get($tname)

    {
        global 
$db;
        
$query $db->query("SELECT * FROM `templates` WHERE sid='{$this->sid}' AND `tname`='{$tname}' LIMIT 1;");
        if(
$db->num_rows($query) != '0')
        {
            
$t $db->fetch_array($query);
            
$tname $t['tname'];
            
$codes $this->replace($t['codes']);
            
//$codes = "<!-- start: $tname : {$this->sid} -->\n{$codes}\n<!-- end: $tname : {$this->sid} -->";
            
$codes str_replace("\\'""'"addslashes($codes));
            return 
$codes;
        }
        else
        {
            die(
"TPLClass:We do not have ".$tname." in our templates table.!");
        }
    } 
Look I use this function to read my template code, when I read one template i will have one more query Now I want to read all templates at the 1st time and after that I use another function to read one of them??
It is possible?
Reply With Quote
  #3 (permalink)  
Old 07-07-10, 05:07 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
PHP Code:

public function get_all() {

 global 
$db;
 
$query $db->query("SELECT `tname` FROM `templates`");
 if(
$db->num_rows($query) > 0) {
    while(
$t $db->fetch_array($query)) {
       
$arr[] = $t['tname'];
   }
  return 
$arr;
  }
return 
false;

To use:
PHP Code:

if(!isset($_GET['get'])) {

$tmps $class->get_all();
if(
$tmps) {
  foreach(
$tmps as $tname) {
        echo 
'<a href="?get=' $tname '">' $tname '</a><br />';
  }
}
}
else {
 
$tmp $_GET['get'];
$class->get($tmp);

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Any Restriction to number of query...? hakeem PHP 5 11-21-08 06:00 PM
WHY..is this only adding 1 half of the query to my Database? davestar057 PHP 1 03-13-08 01:38 PM
code problem throws error matt001 ASP.NET 0 10-23-05 03:30 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 08:16 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.