2016-06-23 11 views
0

whileループで複数の値を追加するには、レベルごとに2つの値を追加できます.1つはidで、もう1つはタイトルです。whileループで複数の値を呼び出す方法

while ($resultSet = mysqli_fetch_assoc($result)) { 
    $id = $resultSet['id']; 
    unset($resultSet['id']); // <-- add this is if you don't want the id in the final set as it's now the key. 
    $res[$id] = $resultSet; 
} 

またはpickする:私のような複数のフィールドには、すでに連想配列であるだけで$resultSetとしてそれらすべてを追加し、誰

$limitStart = $_POST['limitStart']; 
    $limitCount = 15; 
    if(isset($limitStart) || !empty($limitstart)) { 
     $con = mysqli_connect($hostname, $username, $password, $dbname); 
     $query = 'SELECT id, title, caption, description, featured_image, logo, category_sku, industry_sku 
       FROM works ORDER BY title limit '.$limitStart.','.$limitCount .''; 
     $result = mysqli_query($con, $query); 
     $res = array(); 
     while ($resultSet = mysqli_fetch_assoc($result)) { 
     $res[$resultSet['id']] = $resultSet['featured_image']; 
     } 
     echo json_encode($res); 
    } 

答えて

0

を助けてください、サーバーから取得していますいくつかの基本的なPHPを実行し、additinonalフィールドを新しい連想配列として追加します。ここで

captionfeatured_imageを追加することで例を示します

while ($resultSet = mysqli_fetch_assoc($result)) { 
    $res[$resultSet['id']] = ['featured_image'=>$resultSet['featured_image'], 
           'caption' => $resultSet['caption']]; 
    } 
0

たぶんのようなもの:

$res = array(); 
while ($resultSet = mysqli_fetch_assoc($result)) { 
    foreach($resultSet as $key => $value) { 
     $res[$key] = $value; 
    } 
} 

は、トリックを行いますか!

+0

ただ1つのID関連データを取得すると、他にも多くのIDがあります。 –

+0

データの格納方法の構造を投稿できますか? –

0

$ limitStartが14で、$ limitCountが15の場合は、1つのidを返す必要があります。

$limitStart = $_POST['limitStart']; // What is this number? 
$limitCount = 15; 

if文にタイプミスがあります(下記を参照)。

また、ステートメントを準備していないため、コードはSQLインジェクションで脆弱です。

if(isset($limitStart) || !empty($limitStart)) { // Typo here. (small s) 

    $mysqli = mysqli_connect($hostname, $username, $password, $dbname); 

    $sql = "SELECT id, title, caption, description, featured_image, logo, category_sku, industry_sku 
     FROM works ORDER BY title limit ".$limitStart.",".$limitCount.""; 

    $stmt = $mysqli->prepare($sql); // Prepare the statement 
    $stmt->bind_param("ii", $limitStart, $limitCount); // Bind the parameters 
    $stmt->execute(); // Execute the query 

    // Bind the result 
    $stmt->bind_result($id, $title, $caption, $description, $featured_image, $logo, $category_sku, $industry_sku); 
    $result = $stmt->get_result(); 
    $res = array(); 

    while ($resultSet = $result->fetch_assoc()) { 
     $res[$resultSet['id']] = $id; 
    } 

    $stmt->close(); // Close the statement 
    $mysqli->close(); 

    echo json_encode($res); 

} 
関連する問題