1

I have multiple categories for my array such as birthday, wedding and etc. I want to randomly choose 4 items where the category equals birthday. and to echo out those values

  class Cake {
      //properties:database connection and table name
    private $conn;
    private $table_name ='cakes';
    //object properties: one for each field in our table
    public $id;
    public $name;
    public $category;
    public $price;
    public $description;
    public $thumb;
    public $large;


 public function __construct($db){
 //bring in the connection info, store it in this object's $conn property
     $this->conn = $db;

 }
/**
 * the readAll method gets all existing row data
 * @return  all the retrieved row data
 **/
 //if no public or private auto public
 function readAll(){

        $cols = array('id', 'name', 'category', 'price');
     //create a SQL query and run it, storing the data in a var $stmt

        $stmt = $this->conn->prepare('SELECT id, name, category,  description, price, thumb, large FROM '.$this->table_name.' ORDER BY RAND() LIMIT 4'); //this needs to be one or it will be multiple colls

        //run the query, getting the data and stuffing into $stmt
        $stmt->execute();
        //send info back to where readAll() was called
        return $stmt;
 }//end readAll();




     function readWed(){

        $cols = array('id', 'name', 'category', 'price');
     //create a SQL query and run it, storing the data in a var $stmt

        $stmt = $this->conn->prepare('SELECT id, name, category, description, price, thumb, large FROM '.$this->table_name.' WHERE category = Birthday ORDER BY RAND() LIMIT 4'); 

        $stmt->execute();
        //send info back to where readWed() was called
        return $stmt;
 }//end readWed();

here is where I'm trying to out put the values, read all works, but not with the where clause. error reporting won't output either.

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

                include_once('config/spc_database.php');
                include_once('object/cake.php');
                $database = new Database();
                $conn=$database->getConnection();
                $cake =  new Cake($conn);
                $stmt = $cake->readWed();
        ?>

    <div class="left-img">

    <?php  while($row = $stmt->fetch(PDO::FETCH_ASSOC)){   ?>

      <div class="element  hvr-grow">
      <?php echo "HELLOS"; ?>
      <a href="description.php?detailsid=<?php echo $row['id'];?>"> 
      <img class="imgurmob" src="img/
        <?php echo $row['category']; ?>/<?php echo $row['thumb']; ?>" alt="img-sub-category"> 
    </a>
    </div> 
    <?php    } ?>  
Altenrion
  • 764
  • 3
  • 14
  • 35
Hot Java
  • 409
  • 5
  • 16
  • WHERE category = "Birthday" ..... Quotes ???? – devpro Feb 18 '16 at 21:19
  • you mean like ' WHERE category = '.Birthday.' ORDER BY RAND() LIMIT 4'' ? that gives an undefined constant error. – Hot Java Feb 18 '16 at 21:25
  • Birthday is string value??? Or constant variable? – devpro Feb 18 '16 at 21:26
  • change '.Birthday.' to 'Birthday' if it is a valid value in the field, or change it to '.BIRTHDAY.' if it's set as a constant which I doubt. '.Birthday.' means nothing here – JoeCrash Feb 18 '16 at 21:30
  • Error reporting should show `Notice: Use of undefined constant Birthday - assumed 'Birthday'` so something is turning off errors. – AbraCadaver Feb 18 '16 at 21:32

1 Answers1

3

In your where clause Query you are using string value Birthday so you must need to use this value inside the quotes as:

"SELECT id, name, category, description, price, thumb, large FROM ".$this->table_name." WHERE category = 'Birthday' ORDER BY RAND() LIMIT 4"

Example with your code:

$stmt = $this->conn->prepare("
SELECT id, name, category, description, price, thumb, large 
FROM ".$this->table_name." 
WHERE category =  'Birthday' 
ORDER BY RAND() 
LIMIT 4"); 
devpro
  • 16,184
  • 3
  • 27
  • 38