0

I am running into some issues with a query I am trying to make. What I am doing is trying to create a template for multiple pages to display specific images that correlate with that page.

I have individual pages where I am assigning a variable to define the page (you can see where I do this in my code with $page). Then within my database, under solution I am naming the specific records one of the individual page names. For example: if I named a page "Ball", under the database column solution, I would name a few records Ball.

Then within my query, I am trying to count how many records exist that match $page. If the record count is more than 0, I want to display the code in my else statement.

As of now, my database connection is working. I am not getting any errors being printed. You can see my echo $solution_count;. This is showing a 0, but my else-statement is running, which makes 0 sense.

Am I doing anything wrong with how I am trying to count the records? Does anyone see why this isn't working?

DB Table - show create table

projectslider
CREATE TABLE `projectslider` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `solution` varchar(50) NOT NULL,
 `image` text NOT NULL,
 `alt` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

Code on the individual pages:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$page = "enclosures";
include_once("projectSlider.php"); 
?>

Master page - projectSlider.php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$servername = 'localhost';
$username = 'root';
$password = '';

try {   
    $con = new PDO('mysql:host='.$servername.';dbname=mb', $username, $password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //$page = mysql_real_escape_string($page);
    //SQL Call
    $sql_project = "
      SELECT *, COUNT(solution) AS solution_count
      FROM projectslider
      WHERE solution = '. $page .'
    ";

    if ($project_stmt = $con->prepare($sql_project)) {
        $project_stmt->execute();
        $project_rows = $project_stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach ($project_rows as $project_row) {
            $solution_count = $project_row['solution_count'];
            echo $solution_count;
            $project_solution = $project_row['solution'];
            $project_img = $project_row['image'];
            $project_alt = $project_row['alt'];
            $project_img = '<img class="home-comment-profile-pic" src=" '. $project_img .'" alt="' . $project_alt .'">';
            if ($solution_count === 0) {
                echo 'No projects found.';
            } else {
                echo '<section id="solProj">';
                echo '<div class="projSlide">';
                echo $project_img;
                echo '</div>';
                echo '</div>';
            }
        }
    }
}   
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
Paul
  • 3,348
  • 5
  • 32
  • 76
  • 1
    Do you understand you have incorrect concatenation inside query text? – u_mulder Apr 11 '18 at 20:18
  • Also you use `===` which is also checking type of value. `var_dump($solution_count)` and compare it with 0. – u_mulder Apr 11 '18 at 20:19
  • You also either have a missing opening `
    ` or one `
    ` too many.
    – Funk Forty Niner Apr 11 '18 at 20:21
  • When I var_dump($solution_count), I get `string(1) "0"` – Paul Apr 11 '18 at 20:22
  • `WHERE solution = '. $page .'` => `WHERE solution = '$page'`. No idea why you didn't get an error about that. – Funk Forty Niner Apr 11 '18 at 20:22
  • 1
    `//$page = mysql_real_escape_string($page);` that commented out bit bugs me. You're not using that on other pages are you? Seems like you want to do pagination here. – Funk Forty Niner Apr 11 '18 at 20:26
  • `$page = "enclosures"; include_once("projectSlider.php");` those should be reversed and you should have gotten an undefined variable notice about this. Your question is starting to look unclear. – Funk Forty Niner Apr 11 '18 at 20:27
  • @FunkFortyNiner I added that because of a reference in another question, but I didn't see how old it was originally. https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-insert-statement – Paul Apr 11 '18 at 20:28
  • Adding `""` to my query made this work, except the data isn't looping. Am I looping this correctly? – Paul Apr 11 '18 at 20:28
  • @FunkFortyNiner I have to have the `$page` variable first in order for it to read in my master page. If it was the other way it would throw an undefined variable. – Paul Apr 11 '18 at 20:30
  • Try to run a while loop then instead of a `foreach`. – Funk Forty Niner Apr 11 '18 at 20:30
  • @FunkFortyNiner I thought that `fetchAll` was essentially a while loop with PDO? – Paul Apr 11 '18 at 20:32
  • maybe but I never use `foreach`'s, if at all. – Funk Forty Niner Apr 11 '18 at 20:34

1 Answers1

0

One project have many sliders , for this you should have two tables projects and projectsliders with relationship.

projects table:

CREATE TABLE `projects` (

   `id` int(11) NOT NULL AUTO_INCREMENT,
   `project_name` varchar(50) NOT NULL

) 

projectsliders:

CREATE TABLE `projectsliders` (

   `id` int(11) NOT NULL AUTO_INCREMENT,
   `solution` varchar(50) NOT NULL,
   `image` text NOT NULL,
   `alt` text NOT NULL,
   `project_id` int(11),
   PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

fetching projectSliders for one project, Master page - projectSlider.php

Best way to fetching projectsliders for one project is to use OOP you can call a method and pass project id and method should return you a array with projectsliders for this project , but i am improving your code.

error_reporting(E_ALL);
ini_set('display_errors', 1);

$servername = 'localhost';
$username = 'root';
$password = '';

try {   
   $con = new PDO('mysql:host='.$servername.';dbname=mb', $username, 
   $password);
   $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  //$page = mysql_real_escape_string($page);
  //SQL Call
  $sql_project = "SELECT * FROM projectsliders ps inner join projects p
   on  p.id = ps.project_id
   WHERE p.project_name = '. $project_page .'";

  if ($project_stmt = $con->prepare($sql_project)) {

      $project_stmt->execute();

      $count = project_stmt->rowCount();

      if( $count  != 0 ){

         $project_rows = $project_stmt->fetchAll(PDO::FETCH_ASSOC);

         foreach ($project_rows as $project_row) {

            $project_solution = $project_row['solution'];
            $project_img = $project_row['image'];
            $project_alt = $project_row['alt'];
            $project_img = '<img class="home-comment-profile-pic" src=" '. 
            $project_img .'" alt="' . $project_alt .'">';

            echo '<section id="solProj">';
            echo '<div class="projSlide">';
            echo $project_img;
            echo '</div>';
            echo '</div>';

         }

       }else{
         echo 'No projects found.';
       }
     }
  }   
  catch(PDOException $e) {
      echo "Connection failed: " . $e->getMessage();
  }

Individual pages :

error_reporting(E_ALL);
ini_set('display_errors', 1);
$project_page = "enclosures";
include_once("projectSlider.php"); 

I hope that this can help you ,enjoying coding.

Klodian
  • 633
  • 6
  • 18