I am quite confused on this one. While using the switch/case below, if I echo out case 1 my results are as follows:
Metal Cover (300)
Plexi (300)
Flat Cover (200)
Paper Cover (250)
When I echo out the json_encode it returns the following:
{"300":"Plexi (300)","200":"Flat Cover (200)","250":"Paper Cover (250)"}
Why am I losing one of my rows? – thanks
Code
$type = 'cover';
$stt = 1;
$productId = 81;
$results = array();
switch ($type) {
case "cover":
$query = mysql_query("SELECT *
FROM albumcover
WHERE productId = '{$productId}'
");
$results[0] ="None";
switch ($stt){
case 1:
while($row = mysql_fetch_array($query)){
echo $results[$row['price2']] = $row['coverupgrade'] . ' (' . $row['price2'] . ')<br>';
}
break;
case 2:
while($row = mysql_fetch_array($query)){
$results[$row['price3']] = $row['coverupgrade'] .' (' . $row['price3'] . ')';
}
break;
case 3:
while($row = mysql_fetch_array($query)){
$results[$row['price4']] = $row['coverupgrade'] .' (' . $row['price4'] . ')';
}
break;
default :
while($row = mysql_fetch_array($query)){
$results[$row['price1']] = $row['coverupgrade'].' ('.$row['price1'].')';
}
break;
}
echo json_encode($results);
You are loosing a row because you are using the value of
$row['pricex']as the key, and you have two rows where the price is the same, so the later value overwrites the first. In order to transmit the data effectively you will need to change the structure of the JSON:Given the data you show, this should output: