简体   繁体   中英

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.

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)."]'.)\"";
        
      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM