Reputation: 31
I'm trying to set the image src from a variable which is defined in a function with a mysql query but it doesn't get the path.
Here is the function:
function bilderAbfrage($richtigesArray) {
echo "ayy";
$zahl = $GLOBALS["zaehler"];
echo "$zahl";
$pdo = new PDO('mysql:host=localhost;dbname=datagame', 'root', 'root');
$sql = "SELECT * FROM bilder WHERE DateIDFS = $richtigesArray[$zahl]";
foreach ($pdo->query($sql) as $row) {
echo $row['Path']; // here it does print the right path of the image
$bild1 = $row['Path'];
$bild2 = $row['Path2'];
$bild3 = $row['Path3'];
$bild4 = $row['Path4'];
}
}
Here are the images where it doesn't get the variables. I tried it with global but it didn't work:
<div class="row1">
<img src="<?php global $bild1; echo $bild1; ?>" alt="<?php global $bild1; echo $bild1; ?>" id="pic1" class="pic1">
<img src="<?php global $bild2; echo $bild2;?>" id="pic2" class="pic2">
<?php echo "hey";
?>
</div>
<div class="row2">
<img src="<?php global $bild3; echo $bild3; ?>" class="pic3">
<img src="<?php global $bild4; echo $bild4; ?>" class="pic4">
</div>
Upvotes: 1
Views: 48
Reputation: 31
One little error I tried to make the bild1 - bild4 global after I gave them the path.
Correct:
function bilderAbfrage($richtigesArray){
echo "ayy";
$zahl = $GLOBALS["zaehler"];
echo "$zahl";
$pdo = new PDO('mysql:host=localhost;dbname=datagame', 'root', '5718orii');
$sql = "SELECT * FROM bilder WHERE DateIDFS = $richtigesArray[$zahl]";
foreach ($pdo->query($sql) as $row) {
global $bild1, $bild2, $bild3, $bild4;
echo $row['Path'];
$bild1 = $row['Path'];
$bild2 = $row['Path2'];
$bild3 = $row['Path3'];
$bild4 = $row['Path4'];
}
}
Upvotes: 1