[英]php push the results to an array by getting from the table
我有這樣的桌子
listing_id
1
4
345
654
listing documents
listing_id folder filename
000345 full_menu_file testfile098
000345 header_menu_file testfile067
000004 full_menu_file testfile
000001 menu_file testfile567
000004 footer_menu_file testfile76
000004 test_menu_file testfile65
000345 footer_menu_file testfile764
000654 footer_menu_file testfile098
000654 footer_menu_file testfile078
現在,我想制作一個數組並將那些相關數據放入該數組中。 所以最終輸出將是這樣
array(
[1] => Array
(
[listing_id] => 1
[full_menu_file] =>
[header_menu_file] =>
[menu_file] =>
[footer_menu_file] =>
[test_menu_file] =>
)
[1] => Array
(
[listing_id] => 345
[full_menu_file] => testfile067
[header_menu_file] => testfile067
[menu_file] =>
[footer_menu_file] => testfile764
[test_menu_file] =>
)
[2] => Array
(
[listing_id] => 4
[full_menu_file] => testfile
[header_menu_file] =>
[menu_file] =>
[footer_menu_file] => testfile76
[test_menu_file] => testfile65
)
)
所以我做了這樣的PHP代碼
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "listings";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$listing_query = "SELECT * FROM `listings` ORDER BY `listing_id` ASC ";
$cat_array = array();
if( $result = $mysqli->query($listing_query) ) {
while( $obj = $result->fetch_object() ) {
$listing_id = $obj->listing_id;
//Get all the file names
$get_images_name = "SELECT * FROM `listing_documents` WHERE `listing_id` = ".str_pad($listing_id, 6, '0', STR_PAD_LEFT)." ";
if( $img_query = $mysqli->query($get_images_name) ) {
while( $object = $img_query->fetch_object() ) {
if( $object->folder == 'full_menu_file' ) {
$full_menu_file_name = $object->filename;
}
if( $object->folder == 'header_menu_file' ) {
$header_menu_file_name = $object->filename;
}
if( $object->folder == 'menu_file' ) {
$menu_file_name = $object->filename;
}
if( $object->folder == 'footer_menu_file' ) {
$footer_menu_file_name = $object->filename;
}
}
$listing_array['listing_id'] = $listing_id;
$listing_array['full_menu_file_name'] = $object->full_menu_file_name;
$listing_array['header_menu_file'] = $object->header_menu_file;
$listing_array['menu_file'] = $object->menu_file;
$listing_array['footer_menu_file'] = $object->footer_menu_file;
array_push($cat_array, $listing_array);
}
}
print_r($cat_array);
但這並沒有顯示出我需要的結果。 有人可以告訴我該怎么做嗎? 任何建議都是非常可取的。 謝謝
您需要在str_pad
的結果周圍加上引號,以便將其視為字符串,而不是前導零的數字。
$get_images_name = "SELECT * FROM `listing_documents` WHERE `listing_id` = '".str_pad($listing_id, 6, '0', STR_PAD_LEFT)."' ";
您也可以將兩個查詢合並為一個JOIN:
SELECT l.listing_id as l_listing_id, ld.*
FROM listings as l
JOIN listing_documents AS ld ON ld.listing_id = LPAD(l.listing_id, 6, '0')
ORDER BY l_listing_id
沒有聯接的等效查詢為:
SELECT *
FROM listing_documents
WHERE listing_id IN (SELECT LPAD(listing_id, 6, 0) FROM listings)
ORDER BY listing_id
我認為您的問題是str_pad。 您可以改用CONVERT或CAST:
SELECT * FROM `listing_documents` WHERE CAST(listing_id AS INT) = $listing_id
SELECT * FROM `listing_documents` WHERE CONVERT(INT, listing_id) = $listing_id
您的方案對我來說沒有意義。 我不知道為什么要在listing_documents
表中使用填充字符串而不是整數。 但是,使用當前架構,您可以執行以下操作:
$result = $mysqli->query(
<<<'QS'
SELECT listings.listing_id, listing_documents.folder, listing_documents.filename
FROM listings
JOIN listing_documents
ON listing_documents.listing_id LIKE LPAD(CONVERT(listings.listing_id,char),6,'0')
ORDER BY listings.listing_id ASC
QS;
我實際上不確定是否需要為lpad轉換listing_id。 而且,將listing_documents.listing_id
轉換為INT這樣的INT可能更listing_documents.listing_id
,例如ON CONVERT(listing_documents.listing_id,UNSIGNED INTEGER) = listings.listing_id
然后將結果添加到數組中:
$outputData = array();
while ($object=$result->fetch_object()){
if (!isset($outputData[$object->listing_id]))
$outputData[$object->listing_id] = array();
$outputData[$object->listing_id][$object->folder] = $object->filename;
}
通過這種方法,如果一個特定的listing_id
在listing_documents
不具有所有不同的folder
S,然后將鍵跳過。
現在, $outputData
將具有數組鍵為列表ID的數據。
要重置陣列鍵,您可以執行以下操作:
$outputData = array_values($outputData);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.