使用count函数确定if语句中显示的内容 [英] Using the count function to determine what displays in if statement

查看:59
本文介绍了使用count函数确定if语句中显示的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要进行的查询遇到一些问题.我正在做的是尝试为多个页面创建模板,以显示与该页面相关的特定图像.

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.

我在各个页面上分配变量来定义页面(您可以在代码中使用$page看到该位置).然后在我的数据库中,在solution下,我将特定的记录命名为各个页面名称之一.例如:如果我在数据库列solution下将页面命名为球",我将命名为几个记录Ball.

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.

然后在查询中,我试图计算存在与$page相匹配的记录.如果记录计数大于0,我想在我的else语句中显示代码.

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.

到目前为止,我的数据库连接正在工作.我没有收到任何打印错误.您可以看到我的echo $solution_count;.这显示为0,但是我的else-statement正在运行,这使0有意义.

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?

数据库表-显示创建表

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

各个页面上的代码:

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

母版页-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();
}

推荐答案

一个项目有很多滑块,为此,您应该有两个具有关系的表项目和projectslider.

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

项目表:

CREATE TABLE `projects` (

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

) 

projectsliders:

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

获取一个项目的ProjectSliders,母版页-projectSlider.php

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

获取一个项目的projectslider的最佳方法是使用OOP,您可以调用一个方法并传递项目ID,并且method应该返回一个包含该项目的projectsliders的数组,但是我正在改善您的代码.

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();
  }

各个页面:

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.

这篇关于使用count函数确定if语句中显示的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆