English 中文(简体)
php与类似结果合并
原标题:php joining similar results
  • 时间:2012-01-13 12:15:49
  •  标签:
  • php
  • mysql

I m building a php notification system (kinda like facebook s).

It works as should, listing all events the way i want it. but i m trying to get it to understand that if it gets 3+ events on the same post, it will join these events into one notification.

我们现在看着这一点:

User 1 wrote on article-x
User 2 wrote on article-x
user 3 wrote on article-x
user 1 wrote on article-y

instead i want it to be printed like this :

user 1 and  2 others wrote on article-x

Been trying to read up on what i need to do this, but no luck. any pointers greatly appreciated. It s being queried like this:

随附全代码<>。

$sesname = $_SESSION[ user_name ];
$sesid = $_SESSION[ full_name ];
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
$stmt = $dbh->prepare("select * from comments WHERE full_name =  $sesid  and checked = 1  ");
$stmt->bindParam( :full_name , $safe_name, PDO::PARAM_INT, 5);

$stmt->execute();
$result = $stmt -> fetchAll();
foreach( $result as $row ) {
$name = $row[ name ];
$pid = $row[ pid ];
$commentid = $row[ id ];
$fullink = $row[ fullink ];
$datetime = $row[ dt ];

if ($name == $sesname){
} else {
echo "<div id= commentnote ><a href= http://{$fullink} >{$name}</a>kommenterte ditt <a href= {$fullink} >blogginnlegg</a><br><div class= updentry  style= color: red ! important; cursor: pointer; margin-top: -35px; position: absolute; right: 8px; >x</div></div>";
}

}

Tried using your answers underneath, but without luck. Thanks for answering though, i really appreciate it :)

问题回答

将该条变成一个多层面阵容? (例如,结构并不理想,因为我不知道你拥有和需要什么数据)。)

if(count($post[ comments ]) >= 3) {
   echo $post[ user ][0] ."and". (count($post[ comments ])-1). " others wrote on ..";
} else {
   //Just one 
}

也许你可以建立这样两维阵:

Array
(
    [article-x] => Array
                   (
                       [0] => User1
                       [1] => User2
                   )
    [article-y] => Array
                   (
                       [0] => User1
                   )
)

如果你想在公共卫生和社会福利部中这样做的话,就这样做:

$events = array();
foreach( $result as $row ) {
    if( isset($events[$row[ article ]) ){
        $events[$row[ article ]][ counter ]++;
    }else{
        $row[ counter ] = 0;
        $events[$row[ article ]] = $row;
    }
}

foreach( $events as $event ){
    echo "<div id= commentnote >{$event[ user ]}".($event[ counter ]?  and  .$event[ counter ].  others :  )." wrote on {$event[ article ]}</div>";
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签