0

I need looped php data in an html template so I know it has something to do with JSON however not a JSON expert and cannot find much help in searching the web.

$uniqueFranchise_id = array_unique($franchise_id);
$dataArr = '[
    {
        "name": "Dylan",
        "page_link": "https://mypage.com/"
    }
   ]';
foreach($uniqueFranchise_id as $franchise)
{
    $sqlFranchise = "select * from franchise where franchise_id = $franchise";
    $resultFranchise = $conn->query($sqlFranchise);
    if($resultFranchise->num_rows > 0)
    {
        while($rowFranchise = $resultFranchise->fetch_assoc())
        {
           $dataArr = json_decode($data, TRUE);
           $dataArr[] = "['name'=>'".$rowFranchise['name']."', 'page_link'=>'".$rowFranchise['page_link']."']";
           //$json = json_encode($dataArr);
        }
    }
}

$json = json_encode($dataArr);
print_r($dataArr);

But it only appends one row. In fact it deleteds that data that's already in my dataArr and just adds one row from my loop? Maybe I'm approaching this situation completely wrong?

3
  • 1
    Your first $dataArr is not an Array, it is just a String that has a representation of an array. In your while loop, adding $dataArr .= json_decode($data, TRUE); that will append it to your original $dataArr Commented Feb 12, 2019 at 19:57
  • Ok so I need to make the first an associative array? Commented Feb 12, 2019 at 20:04
  • Actually, I don't even think you need to do a json_decode in your while loop. Since $dataArr is already a string anyway, and json_decode converts FROM a string, I'm guessing $data is already a string, just append $data to $dataArr and see what that gives you. Commented Feb 12, 2019 at 20:06

2 Answers 2

4

You set your $dataArr inside the while-loop. So each time the loop is runs, it will be overwritten. Also, it makes more sense and it's much more clear when you handle it as an array (or object) and afterwards convert it to JSON.

$dataArr = array(array('name' => 'Dylan', 'page_link' => 'https://mypage.com/'));

foreach($uniqueFranchise_id as $franchise)
{
    $sqlFranchise = "select * from franchise where franchise_id = $franchise";
    $resultFranchise = $conn->query($sqlFranchise);
    if($resultFranchise->num_rows > 0)
    {
        while($rowFranchise = $resultFranchise->fetch_assoc())
        {
           $dataArr[] = array('name' => $rowFranchise['name'], 'page_link' => $rowFranchise['page_link']);
        }
    }
}

$json = json_encode($dataArr);
echo $json;
Sign up to request clarification or add additional context in comments.

7 Comments

This worked! Thank you. One question though. I just relized. I'm printing $dataArr not $json. I just realized this is maybe a mistake but when I ran it like this it worked perfectly. What is the point of the $json variable?
I did not noticed that. I just updated the code so it shows the json output.
any idea why the results come out like this: {"name":"Dylan","page_link":"https:\/\/mypage.com\/"} why are there backslashes in the page links?
This is a way to escape some characters to avoid breaking the JSON-structure. Don't worry, when running json_decode(), those escape characters will be gone.
Got it. Thanks!
|
2

You shouldn't be building the string up by yourself, you should build the data and then JSON encode the result (comments in code)...

$dataArr = '[
    {
        "name": "Dylan",
        "page_link": "https://mypage.com/"
    }
   ]';
// decode existing JSON to start array
$dataArr = json_decode($data, TRUE);
foreach($uniqueFranchise_id as $franchise)
{
    // Read just the data you need from the table
    $sqlFranchise = "select name, page_link from franchise where franchise_id = $franchise";
    $resultFranchise = $conn->query($sqlFranchise);
    if($resultFranchise->num_rows > 0)
    {
        // Read all of the rows into an array
        $newData = $resultFranchise->fetch_all(MYSQLI_ASSOC);
        // Add in existing data
        $dataArr = array_merge($dataArr, $newData);
    }
}

// Now encode the list of elements into 1 string
echo json_encode($dataArr);

You should also look into prepared statements if this data is not trusted to stop SQL injection.

2 Comments

Thanks, just gave the answer to someone else but I'll look into this. Yea I know about prepared statements. Not doing it here because it's all data hidden from users. You should only use prepared if user is imputing data in a text field right?
Even hidden fields on a form can be changed - trust no one. Some people would suggest to always use prepared statements as it gets you into the habit of doing it this way, but I think you have to make informed decisions as to when you should be using them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.