开发者

How to show only one result from each type in a mysql array php

开发者 https://www.devze.com 2023-02-23 00:53 出处:网络
I have a php array to display information from a database. I want to be able to show items like this ex. Space Quest (3) The \'(3)\' represents the number of times found in the database. Right now it

I have a php array to display information from a database. I want to be able to show items like this ex. Space Quest (3) The '(3)' represents the number of times found in the database. Right now it is pulling the info and listing the results as many times as shows in the database like this ex. Space Quest Space Quest Space Quest which link to the individual stories. Here is my code:

<?php
  $type = $_POST['Type'];

  $result = mysqli_query($cxn, "SELECT * 
                                  FROM Stories 
                                 WHERE Type = '$type'");

if($result < 0) {
  echo "<center><b>No stories are posted for this category yet.</b></center>";
} else {
  while($row = mysqli_fetch_array($result)) {
    $title = $row['Title']; 
    $result2 = mysqli_num_rows($result);
    echo "<FORM ACTION ='browse.php' METHOD ='POST'>
            <INPUT TYPE = 'Submit' name='cool2' id='cool2' VALUE = '$title'>";
    echo "&nbsp;&nbsp;<font color = '#0000ff''>(" . $result2 . ")";
    echo "开发者_StackOverflow社区</form>";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  }
} ?>

This code shows all the items that come from type, but I want to organize the results to show the items only once but then the number of stories in each category.


You should run the following query instead

"SELECT Title,count(*) as cnt FROM Stories WHERE Type = '$type' group by Title"

then you'll only get distinct titles, and the cnt column will contain the number of times it occurs.


The issue is that you're using

while($row = mysqli_fetch_array($result))

This is going to iterate over all of the rows in the result. Instead, if you just want the first one, do

$row = mysqli_num_rows($result); 
$occurrences = mysqli_num_rows($result);
// ...


Well, if I understand correctly, what you want is a grouping function. For example:

select id, title, count( * ) from stories where type = 'ANYTHING' group by id, title

It's that?


This is not answer to your question, but it is useful information to know anyway:

Your SQL query is vunerable to SQL Injection attacks. This is because you are directly inserting user input (the POST parameter) into your SQL query string. For string values, you should use a string escape function, which ensures the string is inserted safely into the query:

$query = "SELECT * FROM Stories WHERE Type = '" . mysqli_real_escape_string($type) . "'";

It is also a good idea to escape any values that you output to your HTML code. This helps prevent Cross-Site Scripting attacks. Use the htmlspecialchars() function to do this:

echo "<INPUT TYPE = 'Submit' name='cool2' id='cool2' VALUE = '" . htmlspecialchars($title) . "'>";
0

精彩评论

暂无评论...
验证码 换一张
取 消