4 random rows uit database een variable geven.
Ik ben bezig met een referentie script voor mijn website. In de database zitten ~50 referenties. Het is de bedoeling dat het script 4 random referenties weergeeft.
mijn script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE check = 1 ORDER BY RAND() limit 4");
$testimonials = mysql_fetch_assoc($gettestimonials);
$name1 = $testimonials['name'[0]]; //hier krijg ik een error
$testimonial1 = $testimonials['testimonial'[0]];
$name2 = $testimonials['name'[1]];
$testimonial2 = $testimonials['testimonial'[1]];
$name3 = $testimonials['name'[2]];
$testimonial3 = $testimonials['testimonial'[2]];
$name4 = $testimonials['name'[3]];
$testimonial4 = $testimonials['testimonial'[3]];
?>
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE check = 1 ORDER BY RAND() limit 4");
$testimonials = mysql_fetch_assoc($gettestimonials);
$name1 = $testimonials['name'[0]]; //hier krijg ik een error
$testimonial1 = $testimonials['testimonial'[0]];
$name2 = $testimonials['name'[1]];
$testimonial2 = $testimonials['testimonial'[1]];
$name3 = $testimonials['name'[2]];
$testimonial3 = $testimonials['testimonial'[2]];
$name4 = $testimonials['name'[3]];
$testimonial4 = $testimonials['testimonial'[3]];
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div class="column_one_half">
<blockquote class="theme"><?php echo $testimonial1; ?></p><p class="blockquote_cite"><?php echo $name1; ?></p></blockquote>
</div>
<div class="column_one_half last">
<blockquote class="theme"><?php echo $testimonial2; ?></p><p class="blockquote_cite"><?php echo $name2; ?></p></blockquote>
</div>
<div class="clearfix"></div>
<div class="column_one_half">
<blockquote class="theme"><?php echo $testimonial3; ?></p><p class="blockquote_cite"><?php echo $name3; ?></p></blockquote>
</div>
<div class="column_one_half last">
<blockquote class="theme"><?php echo $testimonial4; ?></p><p class="blockquote_cite"><?php echo $name4; ?></p></blockquote>
</div>
<div class="clearfix"></div>
<blockquote class="theme"><?php echo $testimonial1; ?></p><p class="blockquote_cite"><?php echo $name1; ?></p></blockquote>
</div>
<div class="column_one_half last">
<blockquote class="theme"><?php echo $testimonial2; ?></p><p class="blockquote_cite"><?php echo $name2; ?></p></blockquote>
</div>
<div class="clearfix"></div>
<div class="column_one_half">
<blockquote class="theme"><?php echo $testimonial3; ?></p><p class="blockquote_cite"><?php echo $name3; ?></p></blockquote>
</div>
<div class="column_one_half last">
<blockquote class="theme"><?php echo $testimonial4; ?></p><p class="blockquote_cite"><?php echo $name4; ?></p></blockquote>
</div>
<div class="clearfix"></div>
Waarom krijg ik hier een error? En wat kan ik doen om het script toch werkend te maken?
(Ik ben heel slecht met MSQL)
Alvast bedankt!
Gewijzigd op 20/01/2014 17:11:49 door Ibrahim A
echo $testimonials['name'];
- SanThe - op 20/01/2014 17:11:34:
echo $testimonials['name'];
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\wamp\www\testimonials.php on line 11
Line 11:
$testimonials = mysql_fetch_assoc($gettestimonials);
Bouw foutafhandeling in om te zien wat er mis gaat.
Ik krijg nu 4 dezelfde referenties. Hoe kan ik de tweede, derde of vierde (name + testimonial) uit het opgehaalde rows krijgen?
In een while() zetten.
- SanThe - op 20/01/2014 17:22:19:
In een while() zetten.
Bedankt het heeft gewerkt! Erwin H ook bedankt!
Ik heb er even een odd/even systeem ingezet, omdat er verschil zit tussen de html regels.
Nieuwe script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE approved = '1' ORDER by RAND() LIMIT 4");
$i = 0;
while($testimonial = mysql_fetch_array($gettestimonials))
{
if($i%2 == 0)
{
echo '<div class="column_one_half">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
}
else
{
echo '<div class="column_one_half last">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
echo '<div class="clearfix"></div>';
}
$i++;
}
?>
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE approved = '1' ORDER by RAND() LIMIT 4");
$i = 0;
while($testimonial = mysql_fetch_array($gettestimonials))
{
if($i%2 == 0)
{
echo '<div class="column_one_half">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
}
else
{
echo '<div class="column_one_half last">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
echo '<div class="clearfix"></div>';
}
$i++;
}
?>
Gewijzigd op 20/01/2014 17:36:11 door Ibrahim A