Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:13:15+00:00 2026-06-08T21:13:15+00:00

I am quite confused on this one. While using the switch/case below, if I

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:13:15+00:00Added an answer on June 8, 2026 at 9:13 pm

    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:

    $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)){
                        $results['data'][] = array('price'=>$row['price2'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price2'] . ')<br>';
                    }
                    break;
                case 2:
                    while($row = mysql_fetch_array($query)){
                        $results['data'][] = array('price'=>$row['price3'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price3'] . ')<br>';
                    }
                    break;
                case 3:
                    while($row = mysql_fetch_array($query)){
                        $results['data'][] = array('price'=>$row['price4'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price4'] . ')<br>';
                    }
                    break;
                default:
                    while($row = mysql_fetch_array($query)){
                        $results['data'][] = array('price'=>$row['price1'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price1'] . ')<br>';
                    }
                    break;
            }
            break;
    }
    echo json_encode($results);
    

    Given the data you show, this should output:

    {
      "0": "none",
      "data": [
        {
          "price": "300",
          "coverupgrade": "Metal Cover (300)"
        },
        {
          "price": "300",
          "coverupgrade": "Plexi (300)"
        },
        {
          "price": "200",
          "coverupgrade": "Flat Cover (200)"
        },
        {
          "price": "250",
          "coverupgrade": "Paper Cover (250)"
        }
      ]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite confused by this one. I've build a read more button using mostly
This is getting me quite confused: I have a small application that uses a
I am quite confused using the Facebook application in the iframe. I granted permissions
I'm new to SVN and I'm quite confused about a problem. I am using
Found myself quite confused today about this. I create a blank repository locally(hg init),
I stumbled uppon this code and I am quite confused on how it compiles
I'm quite lost on this one. I have some acceptence tests running with JUnit4
I've been using config transforms in VS2010 quite a bit lately but am confused
I was trying to figure this out for quite some time.I want a Databind
I am quite confused on whether where to look for the solution on this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.