Using the count function to determine what displays in if statement

前端 未结 1 1067
情书的邮戳
情书的邮戳 2021-01-28 02:00

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

相关标签:
1条回答
  • 2021-01-28 02:18

    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.

    0 讨论(0)
提交回复
热议问题