John Davidson

html - Why does PHP give me different results for my variable?

0 comments
Message:


I can no longer understand the behavior of PHP.


I have a for loop that shows me a button on each line and that allows me to open a modal to display variable parameters:


<?php $postes = R::getAll('SELECT * FROM candidate_postes;');
for ($i = 0; $i < count($postes); $i++){
$var[$i] = ucfirst(strftime('%d %B %Y',strtotime($postes[$i]['date_time_publication'])));
?>
<tr>

<td> <?php echo $postes[$i]['id']; ?> </td>

<td><a class='text-gray-600 text-hover-primary mb-1'><?php echo $postes[$i]['title']; ?></a></td>

<td><a class='text-gray-600 text-hover-primary mb-1'><?php echo $postes[$i]['candidate_numbers']; ?></a></td>

<td><?php echo $var[$i]; ?></td>

<td class="text-end">
<a href="#" class="btn btn-sm btn-light btn-active-light-primary" data-menu-trigger="click">Actions

<span class="svg-icon svg-icon-5 m-0">

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
<path d="M11.4343 12.7344L7.25 8.55005C6.83579 8.13583 6.16421 8.13584 5.75 8.55005C5.33579 8.96426 5.33579 9.63583 5.75 10.05L11.2929 15.5929C11.6834 15.9835 12.3166 15.9835 12.7071 15.5929L18.25 10.05C18.6642 9.63584 18.6642 8.96426 18.25 8.55005C17.8358 8.13584 17.1642 8.13584 16.75 8.55005L12.5657 12.7344C12.2533 13.0468 11.7467 13.0468 11.4343 12.7344Z" fill="currentColor" />
</svg>

</span>

</a>

<div class="menu menu-sub menu-sub-dropdown menu-column menu-rounded menu-gray-600 menu-state-bg-light-primary fw-bold fs-7 w-125px py-4" data-menu="true">

<div class="menu-item px-3">

<a href="#" class="menu-link px-3"


data-id="<?php echo $postes[$i]['id']; ?>"

data-postetitle="<?php echo $postes[$i]['poste']; ?>"

data-postedesc="<?php echo $postes[$i]['desc']; ?>"

data-postecontent="<?php echo $postes[$i]['content']; ?>"

data-postenumber="<?php echo $postes[$i]['candidate_numbers']; ?>"


onclick="

$('#dataid').val(id);

$('#datatitle').text($(this).data('postetitle'));

$('#datatitlebis').text($(this).data('postetitle'));

$('#datatitlethird').text($(this).data('postetitle'));

$('#datadesc').text($(this).data('postedesc'));

$('#datacontent').text($(this).data('postecontent'));

$('#datacandidatesnumber').text($(this).data('postenumber'));

$('#modal_candidate_novalidate').modal('show');

$('#dataid').val($(this).data('id'));

">Candidater</a>

</div>

</div>

</td>
</tr>

<?php } ?>



In the modal, I retrieve these parameters as a variable:


<?php $datatitle = '<span id="datatitle"/>'; echo $datatitle; ?>

So far so good.
However, I have a BBCODE stored in my database that I would like to display correctly using the code below:


    <div class="fv-row mb-10">
<label class="col-lg-8 fw-bold text-muted">A propos du rôle :</label>
<div class="col-lg-8 fv-row">
<span class="fw-bold text-gray-800 fs-6">
<?php
$datacontent = '<span id="datacontent"/>';
require_once "../../libs/bbcode/SBBCodeParser.php";
$parser = new \SBBCodeParser\Node_Container_Document();
echo $parser->parse($datacontent)
->detect_links()
->get_html();
?>
</span>
</div>
</div>

The problem is that when I load my page, it is not the content of my database that appears, but only <span id="datacontent"/>.


datacontent


I can’t understand the behavior of PHP, because when I simply echo my variable, it’s the non-parser code that is displayed.


Does anyone have an explanation and a solution to this phenomenon?



My Answer:
It is possible that the variable you are trying to display in the modal is being modified or re-assigned within the loop, causing it to have different values each time the loop runs. Make sure that you are not inadvertently changing the value of the variable within the loop, or that there are no other parts of your code that are affecting the variable's value.

If you are still having trouble understanding the behavior of PHP, you may want to consider adding some debugging statements to your code to help you track the value of the variable as the loop runs. This can help you identify any unexpected changes to the variable's value and troubleshoot the issue more effectively.

Rate this post

5 of 5 based on 8510 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us