If the structures of multiple tables are different, it seems impossible to query the results of multiple tables together! For example, Table
If the structures of multiple tables are different, it seems impossible to query the results of multiple tables together!
For example, table:
A_id |
Title |
Is_open |
Last_update |
2 |
Test 1 |
1 |
130225252 |
Table B:
B _id |
Name |
Is_check |
Last_update |
2 |
B Test 2 |
0 |
156523322 |
In this way, two tables with different structures can be displayed in a list.
First, we create a variable: $ moretable
$ Moretable = array ('a table' => array ('where' => 'is _ open', 'zone '=> 'Title as name, a_id as checkid, last_update ', 'URL' => 'a1. php', 'keyword' => 'title'), 'B table' => array ('where' => 'is _ check ', 'zone '=> 'name as name, B _id as checkid, last_update', 'URL' => 'b1. php', 'keyword' => 'name '))
Then SQL. obtain the total number of data:
Create a function in php first
function get_alltable_count($table=array(),$type=0,$user_id,$keywords=''){$info=0;if (is_array($table)){foreach ($table as $key=>$val){$sql ="SELECT COUNT(*) FROM ".$GLOBALS['sdk']->table($key)." WHERE $val[where] = '$type' "; if (!empty($keywords)) { $sql.="AND $val[keyword] LIKE '%" . mysql_like_quote($keywords) . "%' "; }$sql .="AND user_id = '$user_id' ";$info += $GLOBALS['db']->getOne($sql);}return $info;}else {return $info;}}
Then, obtain the total number of data:
$record_count = get_alltable_count($GLOBALS['moretable'],0,$user_id,$filter['keyword']);
Then, obtain the data:
function get_alltable_infos($table=array(),$type=0,$user_id,$num = 10, $start = 0, $keywords=''){$arr=array();if (is_array($table)){foreach ($table as $key=>$val){$sql = "SELECT $val[zone] FROM ".$GLOBALS['sdk']->table($key)." WHERE $val[where] = '$type' "; if (!empty($keywords)) { $sql.="AND $val[keyword] LIKE '%" . mysql_like_quote($keywords) . "%' "; }$sql.="AND user_id = '$user_id' ORDER BY last_update DESC";$res=$GLOBALS['db']->query($sql);while ($row = $GLOBALS['db']->fetchRow($res)) {$row['url'] = $val['url']."?act=edit&id=".$row['checkid'];$row['last_update'] = local_date($GLOBALS['_CFG']['time_format'],$row['last_update']);;$arr[]=$row;}}$arr=array_slice($arr,$start,$num);}return $arr;}
Paging functions can be written by yourself. with the total number of data pages, it is easy to write.