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

  • Home
  • SEARCH
  • 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 8054859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:14:32+00:00 2026-06-05T08:14:32+00:00

I am trying to create loop inside of a HEREDOC in php. I know

  • 0

I am trying to create loop inside of a HEREDOC in php. I know that it is not possible to do so, but it was the best way for me to tell what I need help for..
This is my code:

<?
$sth = $dbh->prepare("SELECT field1, field2 FROM table WHERE id = '".$_GET['id']."'");
$sth->execute();
echo <<<HERE_DOC
    [
    while($row = $sth->fetch()){ 
        echo '{"optionValue":"'.$row['field1'].'", "optionDisplay": "'.$row['field2'].'"},'; 
    }
    ]
HERE_DOC;
?>

and it tells me this

 Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /public_html/test.php on line 7

I think it returns this because it is not possible to create a loop inside a HEREDOC – but how do I do this otherwise..?

The script is driving by AJAX with this script:

<script type="text/javascript">
$(function(){
  $("select#state").change(function(){
    $.getJSON("test.php",{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("select#city").html(options);
    })
  })
})
</script>

and the original test.php is like this:

if ($_GET['id'] == "1234") {
  echo <<<HERE_DOC
    [
    {"optionValue":"000", "optionDisplay": "a"},
    {"optionValue":"300", "optionDisplay": "s"},
    {"optionValue":"600", "optionDisplay": "d"},
    {"optionValue":"700", "optionDisplay": "cr"},
    {"optionValue":"500", "optionDisplay": "Gı"},
    {"optionValue":"400", "optionDisplay": "K"},
    {"optionValue":"800", "optionDisplay": "S"},
    {"optionValue":"900", "optionDisplay": "ik"},
    {"optionValue":"350", "optionDisplay": "Tu"}
    ]
HERE_DOC;
else if ($_GET['id'] == "2345") { etc......

The thing is that I already have all the data in an SQL database, and I would like to just return the data based on the id from previous select list since there is a lot of options.

  • 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-05T08:14:34+00:00Added an answer on June 5, 2026 at 8:14 am

    Don’t make the JSON yourself, use json_encode.

    <?
    $json = array();
    $sth = $dbh->prepare("SELECT field1, field2 FROM table WHERE id = '".$_GET['id']."'");
    $sth->execute();
    while($row = $sth->fetch(PDO::FETCH_ASSOC)){ // added PDO::FETCH_ASSOC
        $json[] = array(
            'optionValue' => $row['field1'],
            'optionDisplay' => $row['field2']
        );
    }
    echo json_encode($json);
    ?>
    

    P.S. Don’t use $_GET directly like that. You’re vulnerable to SQL injection. You’re already using PDO, use it correctly. You should do it like this:

    $sth = $dbh->prepare("SELECT field1, field2 FROM table WHERE id = ?");
    $sth->execute(array($_GET['id']));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to show ProgressDialog, but getting: Can't create handler inside thread that has not
I am trying to create lambdas inside a loop that iterates over a list
I'm trying to create a loop that creates a series of objects that contains
I'm trying to create a for loop with NSDates, so that the loop would
I'm trying to create functions inside of a loop: functions = [] for i
I'm trying to create a simple loop that adds a random number of stars,
I'm trying to create a loop than continues to take input until the input
I am trying to create a loop function based on the time. After the
I am running a loop, and I am trying to create a variable each
I am trying to create a batch script for my Windows machine that loops

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.