Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

php echo html tags that contain a variable to be defined later

I generate buttons for each element of my database and each button contains a currentSlide() Function which i need to define the slider i want it to open predefined. I am using an array $buttonnums and define each element as "-1" but i want to define it later to another number and apply the change to the html in the same way we say class = " < ?php $var ? >" and afterwards we set the $var to something.

while($row = $result->fetch_assoc()) {
            array_push($buttonnums,"-1");
            end($buttonnums);
            echo "<button class="tablinks" onclick="OpenCoffeeType(event,". $row["id"]."); currentSlide(<?php echo $buttonnums[".key($buttonnums)."]?>)"";
            if($count===0)
            {
                echo " id="defaultOpen"";
                $count++;
            }
        echo ">". $row["title"]. " </button>";
    }

and afterwards i set their values here:

while($row2 = $result2->fetch_assoc()) {
                    if ($count===0)
                    {
                        $buttonnums[$count2] = $counter;
                    }
                    echo "<span class="dot periexomenakatigorias"  onclick="currentSlide(".$counter.")">".$row2["title"]."</span>";
                    $counter++;
                }

The result I get is currentSlide(<php echo $buttonnums[0]?>) instead of getting each time the value of $buttonnums[0]. I upload a picture too here to make it more clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your echo command is wrong. You echo another <?PHP tag with another echo. You should echo the actual variable instead.

echo "<button class="tablinks" onclick="OpenCoffeeType(event,". $row["id"]."); currentSlide(<?php echo $buttonnums[".key($buttonnums)."]?>)"";

Should be something like this

echo "<button class="tablinks" onclick="OpenCoffeeType(event,". $row["id"]."); currentSlide(.'$buttonnums[".key($buttonnums)."]'.)"";
        
      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...