Jump to content

[SOLVED] PHP Query


CanMan2004

Recommended Posts

Hi all

 

I have a database which holds events, the database looks like

 

ID  TYPE  DATE FROM

1    1      01-02-2007

2    1      01-02-2007

3    2      01-02-2007

 

What I want to do is run the query below

 

$sql = "SELECT * FROM `events` WHERE `datefrom` = '01-02-2007";
$query = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
print $row['type'];
print "<br>";
}

 

The query above currently returns the result

 

1

1

2

 

What I want to do is to change the above query and say

 

if all the rows returned all have 1 as there $row['type'] value then print "one";

 

if all the rows returned all have 2 as there $row['type'] value then print "two";

 

and if all the rows returned all have a mix of 1 and 2 as there $row['type'] value then print "mixed of one and two";

 

Does this make much sense?

 

Any help would be great

 

Thank in advance

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/37683-solved-php-query/
Share on other sites

I don't really understand how they could have a mix but something like this may do....

 

while ($row = mysql_fetch_array($query))
{
  switch ($row['type']) {
    case 1: echo "one" ; break;
    case 2: echo "two"; break;
    default: echo "mixed of one and two" ; break;
  }
print "<br>";
}

Link to comment
https://forums.phpfreaks.com/topic/37683-solved-php-query/#findComment-180237
Share on other sites

Hi

 

Basically here is a snippet of my db

 

ID  TYPE  DATE FROM

1    1      01-02-2007

2    1      01-02-2007

3    2      01-02-2007

 

So with my query all the above are returned, which is a mix of 1's and 2's, so if that happens, it should print "mixed" just once. On the otherhand, if the database looked like

 

ID  TYPE  DATE FROM

1    1      01-02-2007

2    1      01-02-2007

 

Then it would print "one" as all the rows returned have 1 under there type.

 

Does that clear any questions up?

 

Thanks so far

Link to comment
https://forums.phpfreaks.com/topic/37683-solved-php-query/#findComment-180256
Share on other sites

<?php
$sql = "SELECT *
       FROM `events`
       WHERE `datefrom` = '01-02-2007
       GROUP BY `type`";
$query = @mysql_query($sql,$connection) or die(mysql_error());

if (mysql_num_rows($query)==0) {
   echo "No results returned.";
} elseif (mysql_num_rows($query)>1) {
   echo "Mixed of one and two.";
} else {
   $result = mysql_fetch_assoc($query);
   if ($result['type']=="1") {
      echo "one";
   } else {
       echo "two";
   }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/37683-solved-php-query/#findComment-180261
Share on other sites

A bit messy but should do the trick.

 

<?php

  function whattype($arr) {
    
    $tmp = array_count_values($arr);

    if ($tmp[1] > 0 && $tmp[2] > 0) {
      return "mixed";
    } else {
      if ($tmp[1] > 0) {
        return "one";
      } else {
        if ($tmp[2] > 0) {
          return "two";
        }  
      }  
    }
  }

  $sql = "SELECT * FROM `events` WHERE `datefrom` = '01-02-2007";
  $query = @mysql_query($sql,$connection) or die(mysql_error());
  while ($row = mysql_fetch_array($query)) {
    $tmp[] = $row['type'];
  }

  echo whattype($tmp)."<br />";

?>

Link to comment
https://forums.phpfreaks.com/topic/37683-solved-php-query/#findComment-180270
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.