Using PHP strings as element ID in HTML












0















I’m trying to dynamically create <p> elementss with different ids to "fill" them with JavaScript’s innerHTML property.



Firstly I try to create 50 times:



echo "<p id=space1></p>";


But the number up going like space2, space3 etc.



I made this loop to generate the incrementing numbers and put it together with the string, and echo it:



for ($x = 1; $x <= 50; $x++) {
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
}


But when I run it and check the HTML code on the page, all I see is:



<p id=""></p>


But when I check the arrays by directly echoing them, the names get displayed correctly.



So how can I use my php strings as element ID in HTML?










share|improve this question





























    0















    I’m trying to dynamically create <p> elementss with different ids to "fill" them with JavaScript’s innerHTML property.



    Firstly I try to create 50 times:



    echo "<p id=space1></p>";


    But the number up going like space2, space3 etc.



    I made this loop to generate the incrementing numbers and put it together with the string, and echo it:



    for ($x = 1; $x <= 50; $x++) {
    $xarray = array($x => "space" . $x);
    echo "<p id=$xarrray[$x]></p>";
    }


    But when I run it and check the HTML code on the page, all I see is:



    <p id=""></p>


    But when I check the arrays by directly echoing them, the names get displayed correctly.



    So how can I use my php strings as element ID in HTML?










    share|improve this question



























      0












      0








      0


      1






      I’m trying to dynamically create <p> elementss with different ids to "fill" them with JavaScript’s innerHTML property.



      Firstly I try to create 50 times:



      echo "<p id=space1></p>";


      But the number up going like space2, space3 etc.



      I made this loop to generate the incrementing numbers and put it together with the string, and echo it:



      for ($x = 1; $x <= 50; $x++) {
      $xarray = array($x => "space" . $x);
      echo "<p id=$xarrray[$x]></p>";
      }


      But when I run it and check the HTML code on the page, all I see is:



      <p id=""></p>


      But when I check the arrays by directly echoing them, the names get displayed correctly.



      So how can I use my php strings as element ID in HTML?










      share|improve this question
















      I’m trying to dynamically create <p> elementss with different ids to "fill" them with JavaScript’s innerHTML property.



      Firstly I try to create 50 times:



      echo "<p id=space1></p>";


      But the number up going like space2, space3 etc.



      I made this loop to generate the incrementing numbers and put it together with the string, and echo it:



      for ($x = 1; $x <= 50; $x++) {
      $xarray = array($x => "space" . $x);
      echo "<p id=$xarrray[$x]></p>";
      }


      But when I run it and check the HTML code on the page, all I see is:



      <p id=""></p>


      But when I check the arrays by directly echoing them, the names get displayed correctly.



      So how can I use my php strings as element ID in HTML?







      php html arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 1:19









      Funk Forty Niner

      1




      1










      asked Jan 1 at 23:55









      SnoenySnoeny

      92




      92
























          3 Answers
          3






          active

          oldest

          votes


















          -1














          You have a typo in retrieving your array.



          $xarray = array($x => "space" . $x);
          echo "<p id=$xarrray[$x]></p>";


          Your first line it's called $xarray and your second line it's called $xarrray (extra r)



          $xarray=array();
          for ($x = 1; $x <= 50; $x++) {
          $xarray[$x] = "space" . $x;
          echo '<p id="{$xarray[$x]}"></p>';
          }





          share|improve this answer
























          • Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

            – Snoeny
            Jan 2 at 0:36











          • It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

            – Second2None
            Jan 2 at 0:38








          • 1





            @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

            – ivanivan
            Jan 2 at 0:42



















          -2














          <p id="<?php echo $php_variable; ?>"></p>


          This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.






          share|improve this answer































            -2














            Try this:



               $xarray=array();
            for ($x = 1; $x <= 50; $x++)
            {
            $xarray[$x] = "space" . $x;
            echo "<p id='" . $xarray[$x] . "'></p>";
            }





            share|improve this answer


























            • tried it and still just get this as html code: <p id=""></p>

              – Snoeny
              Jan 2 at 0:29













            • @Snoeny there was a typo, sorry, try again now :)

              – Ass3mbler
              Jan 2 at 0:35











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999864%2fusing-php-strings-as-element-id-in-html%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            -1














            You have a typo in retrieving your array.



            $xarray = array($x => "space" . $x);
            echo "<p id=$xarrray[$x]></p>";


            Your first line it's called $xarray and your second line it's called $xarrray (extra r)



            $xarray=array();
            for ($x = 1; $x <= 50; $x++) {
            $xarray[$x] = "space" . $x;
            echo '<p id="{$xarray[$x]}"></p>';
            }





            share|improve this answer
























            • Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

              – Snoeny
              Jan 2 at 0:36











            • It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

              – Second2None
              Jan 2 at 0:38








            • 1





              @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

              – ivanivan
              Jan 2 at 0:42
















            -1














            You have a typo in retrieving your array.



            $xarray = array($x => "space" . $x);
            echo "<p id=$xarrray[$x]></p>";


            Your first line it's called $xarray and your second line it's called $xarrray (extra r)



            $xarray=array();
            for ($x = 1; $x <= 50; $x++) {
            $xarray[$x] = "space" . $x;
            echo '<p id="{$xarray[$x]}"></p>';
            }





            share|improve this answer
























            • Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

              – Snoeny
              Jan 2 at 0:36











            • It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

              – Second2None
              Jan 2 at 0:38








            • 1





              @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

              – ivanivan
              Jan 2 at 0:42














            -1












            -1








            -1







            You have a typo in retrieving your array.



            $xarray = array($x => "space" . $x);
            echo "<p id=$xarrray[$x]></p>";


            Your first line it's called $xarray and your second line it's called $xarrray (extra r)



            $xarray=array();
            for ($x = 1; $x <= 50; $x++) {
            $xarray[$x] = "space" . $x;
            echo '<p id="{$xarray[$x]}"></p>';
            }





            share|improve this answer













            You have a typo in retrieving your array.



            $xarray = array($x => "space" . $x);
            echo "<p id=$xarrray[$x]></p>";


            Your first line it's called $xarray and your second line it's called $xarrray (extra r)



            $xarray=array();
            for ($x = 1; $x <= 50; $x++) {
            $xarray[$x] = "space" . $x;
            echo '<p id="{$xarray[$x]}"></p>';
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 0:30









            Second2NoneSecond2None

            1,1281416




            1,1281416













            • Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

              – Snoeny
              Jan 2 at 0:36











            • It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

              – Second2None
              Jan 2 at 0:38








            • 1





              @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

              – ivanivan
              Jan 2 at 0:42



















            • Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

              – Snoeny
              Jan 2 at 0:36











            • It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

              – Second2None
              Jan 2 at 0:38








            • 1





              @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

              – ivanivan
              Jan 2 at 0:42

















            Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

            – Snoeny
            Jan 2 at 0:36





            Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.

            – Snoeny
            Jan 2 at 0:36













            It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

            – Second2None
            Jan 2 at 0:38







            It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes

            – Second2None
            Jan 2 at 0:38






            1




            1





            @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

            – ivanivan
            Jan 2 at 0:42





            @Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc

            – ivanivan
            Jan 2 at 0:42













            -2














            <p id="<?php echo $php_variable; ?>"></p>


            This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.






            share|improve this answer




























              -2














              <p id="<?php echo $php_variable; ?>"></p>


              This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.






              share|improve this answer


























                -2












                -2








                -2







                <p id="<?php echo $php_variable; ?>"></p>


                This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.






                share|improve this answer













                <p id="<?php echo $php_variable; ?>"></p>


                This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 23:59









                JoshuaJoshua

                1025




                1025























                    -2














                    Try this:



                       $xarray=array();
                    for ($x = 1; $x <= 50; $x++)
                    {
                    $xarray[$x] = "space" . $x;
                    echo "<p id='" . $xarray[$x] . "'></p>";
                    }





                    share|improve this answer


























                    • tried it and still just get this as html code: <p id=""></p>

                      – Snoeny
                      Jan 2 at 0:29













                    • @Snoeny there was a typo, sorry, try again now :)

                      – Ass3mbler
                      Jan 2 at 0:35
















                    -2














                    Try this:



                       $xarray=array();
                    for ($x = 1; $x <= 50; $x++)
                    {
                    $xarray[$x] = "space" . $x;
                    echo "<p id='" . $xarray[$x] . "'></p>";
                    }





                    share|improve this answer


























                    • tried it and still just get this as html code: <p id=""></p>

                      – Snoeny
                      Jan 2 at 0:29













                    • @Snoeny there was a typo, sorry, try again now :)

                      – Ass3mbler
                      Jan 2 at 0:35














                    -2












                    -2








                    -2







                    Try this:



                       $xarray=array();
                    for ($x = 1; $x <= 50; $x++)
                    {
                    $xarray[$x] = "space" . $x;
                    echo "<p id='" . $xarray[$x] . "'></p>";
                    }





                    share|improve this answer















                    Try this:



                       $xarray=array();
                    for ($x = 1; $x <= 50; $x++)
                    {
                    $xarray[$x] = "space" . $x;
                    echo "<p id='" . $xarray[$x] . "'></p>";
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 0:34

























                    answered Jan 2 at 0:01









                    Ass3mblerAss3mbler

                    3,11611617




                    3,11611617













                    • tried it and still just get this as html code: <p id=""></p>

                      – Snoeny
                      Jan 2 at 0:29













                    • @Snoeny there was a typo, sorry, try again now :)

                      – Ass3mbler
                      Jan 2 at 0:35



















                    • tried it and still just get this as html code: <p id=""></p>

                      – Snoeny
                      Jan 2 at 0:29













                    • @Snoeny there was a typo, sorry, try again now :)

                      – Ass3mbler
                      Jan 2 at 0:35

















                    tried it and still just get this as html code: <p id=""></p>

                    – Snoeny
                    Jan 2 at 0:29







                    tried it and still just get this as html code: <p id=""></p>

                    – Snoeny
                    Jan 2 at 0:29















                    @Snoeny there was a typo, sorry, try again now :)

                    – Ass3mbler
                    Jan 2 at 0:35





                    @Snoeny there was a typo, sorry, try again now :)

                    – Ass3mbler
                    Jan 2 at 0:35


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999864%2fusing-php-strings-as-element-id-in-html%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory