4 random rows uit database een variable geven.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ibrahim A

Ibrahim A

20/01/2014 17:07:38
Quote Anchor link
Hey mensen,

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)
PHP script in nieuw venster Selecteer het PHP script
1
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]];
?>

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
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>

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
 
PHP hulp

PHP hulp

07/05/2024 14:58:44
 
- SanThe -

- SanThe -

20/01/2014 17:11:34
Quote Anchor link
echo $testimonials['name'];
 
Ibrahim A

Ibrahim A

20/01/2014 17:13:55
Quote Anchor link
- 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);
 
- SanThe -

- SanThe -

20/01/2014 17:16:00
Quote Anchor link
Je query zal mislukt zijn.
Bouw foutafhandeling in om te zien wat er mis gaat.
 
Erwin H

Erwin H

20/01/2014 17:18:09
Quote Anchor link
CHECK is een reserved word in mysql. Niet gebruiken dus als kolom naam. Daarom mislukt je query en daarom krijg je die foutmelding. Als je correcte foutafhandeling inbouwt voor je queries kan je dit soort zaken zelf vinden.

mysql reserved words: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
 
Ibrahim A

Ibrahim A

20/01/2014 17:21:09
Quote Anchor link
Dank u beiden. We zijn er bijna!
Ik krijg nu 4 dezelfde referenties. Hoe kan ik de tweede, derde of vierde (name + testimonial) uit het opgehaalde rows krijgen?
 
- SanThe -

- SanThe -

20/01/2014 17:22:19
Quote Anchor link
In een while() zetten.
 
Ibrahim A

Ibrahim A

20/01/2014 17:35:13
Quote Anchor link
- 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)
PHP script in nieuw venster Selecteer het PHP script
1
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++;
    }

?>
Gewijzigd op 20/01/2014 17:36:11 door Ibrahim A
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.