0
Hello, I’m new in PHP and I came across a problem: How to pass a PHP variable ($id) that is inside an "a" tag into a Modal?
Here is the code:
<?php
require '../SisCellCenter/autoloader.php';
use VisaoJR\Cellcenter\ClassPHP\Products;
$products = new Products();
?>
<div class="content">
<div class="new-arrivals-w3agile">
<div class="container">
<!-- <h2 class="tittle">Destaques</h2> -->
<?php
$featuredProducts = $products->viewFeatureds();
$rand_keys = array_rand($featuredProducts, sizeOf($featuredProducts)); //escolhe produtos em destaque aleatoriamente
$max=0;
if(sizeOf($featuredProducts)>9){$max = 9;} else{$max = sizeOf($featuredProducts);} //exibe no máximo 9 produtos em destaque
for ($i=0; $i < $max; $i++) {
?>
<div class="arrivals-grids">
<div class="col-md-3 arrival-grid simpleCart_shelfItem">
<div class="grid-arr">
<div class="grid-arrival">
<figure>
<a class="new-gri" id="featured" data-toggle="modal" data-target="#featuredModal" value="<?php $id = $i; ?>">
<div class="grid-img">
<?php echo '<img src="images/Produtos/'.$featuredProducts[$rand_keys[$i]]->image.'">'; ?>
</div>
<div class="grid-img">
<?php echo '<img src="images/Produtos/'.$featuredProducts[$rand_keys[$i]]->image.'">'; ?>
</div>
</a>
</figure>
</div>
<div class="ribben">
<p>NEW</p>
</div>
<div class="ribben1">
<p>SALE</p>
</div>
<div class="women">
<h6><center><?php echo $featuredProducts[$rand_keys[$i]]->name; ?></center></h6>
<span class="size"><?php echo $featuredProducts[$rand_keys[$i]]->brand; ?></span>
</div>
</div>
</div>
<!-- <div class="clearfix"></div> -->
</div>
<?php }?>
</div>
</div>
</div>
<!--content-->
<!--Modal produtos em destaque-->
<div class="modal fade" id="featuredModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content modal-info">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<div class="news-gr">
<div class="col-md-5 new-grid1">
<?php echo '<img src="images/Produtos/'.$featuredProducts[$id]->image.'" class="img-responsive" alt="">'; ?>
</div>
<div class="col-md-7 new-grid">
<h5><?php echo $featuredProducts[$id]->name; ?></h5>
<h6><?php echo $featuredProducts[$id]->brand; ?></h6>
<span><?php echo $featuredProducts[$id]->description; ?></span>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
From now on, I thank anyone who can help me!
You have to use javascritp to solve this, not php.
– Yure Pereira
And the <a> tag does not have the value attribute.
– Yure Pereira
Because @Yurepereira, I couldn’t think of anything to solve this. Let’s say I’m pretty new in this area
– Leo3102
Which variable you want to pass ?
– Chefe Druida
Take a look at this answer and see if you can help: https://answall.com/questions/58958/passar-vari%C3%A1vel-php-para-javascript
– Chefe Druida
What is the variable?
$id
? Where in the modal do you want to use it? See, Information is as important as code, try to put as much information and clearly so that people can help you. Hug!– Andrei Coelho
Yes, it is the $id variable, because with it I can access the product information. or so I if I can I would like to pass this here: $featuredProducts[$rand_keys[$i]], which is the product with all fields
– Leo3102
I have also used it this way: https://answall.com/questions/129807/enviar-um-valor-para-o-modal
– Chefe Druida
@Leo3102, but that variable she’s in a loop, you’ll want as many as you can?
– Andrei Coelho
Ah ta... I know... pera ae that I will formulate the answer
– Andrei Coelho