0

我在 jQuery Mobile 中有一个分页列表视图,它正在从 DB 表 1“艺术家”创建艺术家缩略图网格,其中包含 143 个艺术家姓名和初始图片和生物(这部分有效)。然后表2“艺术品”有所有剩余的(1006)艺术品。

如何拉入关联的艺术家作品,我应该使用联接还是引用“artistId”(Artworks 表中的外键)而不是“id”(Artworks 表中的主键)?

DB结构和数据示例示例:

表 1 - 艺术家

id, artistId, firstName, lastName,  bio, category, officePhone, audio, email, city, picture
14, 1-2alas, 2alas, ,2Alas Is a collective., Mural ,555-1212, 2Alas.mp3, 2alas@gmail.com, Miami, IMG_2521.jpg

表 2 - 艺术品

id,  artistId,  firstName,  lastName,  imageName , latitude,  longitude , category
23, 1-2alas, 2alas, , IMG_2521.jpg, 25.800575, -80.200933, Mural
24, 1-2alas, 2alas, , IMG_2596.jpg, 25.800775, -80.196627, Mural
25, 1-2alas, 2alas, , IMG_2760.jpg, 25.799992, -80.196270, Mural

代码:

    <?php

error_reporting(0);
header('Access-Control-Allow-Origin: *');
$con=mysqli_connect("localhost","usr","pass","dbname");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$perPage    = $_REQUEST['listomatic']['perPage'];
$listOffset = $_REQUEST['listomatic']['listOffset'];
$searchTerm = $_REQUEST['listomatic']['searchTerm'];

if ($searchTerm) {
    $sql = "SELECT SQL_CALC_FOUND_ROWS *
            FROM artists
            WHERE firstName LIKE '%$searchTerm%'
            ORDER BY firstName ASC
            LIMIT $listOffset, $perPage";
} else {
    $sql = "SELECT SQL_CALC_FOUND_ROWS *
            FROM artists
            ORDER BY firstName ASC
            LIMIT $listOffset, $perPage";
}
$result = mysqli_query($con, $sql);

$resultNumRows = mysqli_query($con, 'SELECT FOUND_ROWS() as foundRows');
$rowFoundRows = mysqli_fetch_array($resultNumRows);
$iFoundRows = $rowFoundRows['foundRows'];

while($row = mysqli_fetch_array($result)) {
    $sDate = date('m/d/Y', strtotime($row['date']));
    $sPicture = ($row['picture']);
    $sFirstName = ($row['firstName']);
    $sLastName = ($row['lastName']);
    $sId = ($row['id']);
    // Listomatic requires the "total" field to show/hide the "Show More" button
    $aData['total'] = $iFoundRows; 
    // The following is sample data in the artist table that you want to display
    $aData['artists'][] = array('date' =>  $sDate,
                                'picture' => $sPicture,
                                'firstName' => $sFirstName,
                                'lastName' => $sLastName,
                                'id' => $sId
                                );
}
echo json_encode($aData);
exit;
?>
4

0 回答 0