Slučajni odabir i tražilica

Kad sam radio web napravio sam si i rubriku slučajnog odabira vijesti iz baze i to sve funkcioniralo dobro. Danas odlučio ugraditi tražilicu i kad bi u polje upisao neki pojam i kliknuo da traži na mjestu gdje se nalazi taj slučajni odabri izbacilo bi mi niz poruka:

Ne kužim šta mu smeta s povezanosti s tražilicom, jednostavno mi taj id u redku 9 mora biti, neznam na koji način bi to drukčije izveo…

evo koda slučajnih vijest:

<span class="syntaxhtml"><br /><span class="syntaxdefault"><?php<br /></span><span class="syntaxkeyword">include(</span><span class="syntaxstring">"dbkon.php"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">$ispis&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"SELECT&nbsp;*&nbsp;FROM&nbsp;bazetina"</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span class="syntaxdefault">$redak&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">mysql_fetch_array</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$ispis</span><span class="syntaxkeyword">)){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">]=</span><span class="syntaxdefault">$redak</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">id</span><span class="syntaxkeyword">];<br />}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span class="syntaxdefault">$redak</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span class="syntaxdefault">$ispis</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">count</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$b</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$d</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$f</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$g</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$h</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">$citaj&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"SELECT&nbsp;*&nbsp;FROM&nbsp;bazetina&nbsp;WHERE&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$b</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$d</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$f</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$g</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$h</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'&nbsp;or&nbsp;id='"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"'"</span><span class="syntaxkeyword">);<br />echo</span><span class="syntaxstring">"<div&nbsp;id='slucajno'>"</span><span class="syntaxkeyword">;<br />while(</span><span class="syntaxdefault">$redak&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">mysql_fetch_array</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$citaj</span><span class="syntaxkeyword">)){<br />include(</span><span class="syntaxstring">"zamjena.php"</span><span class="syntaxkeyword">);&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />echo</span><span class="syntaxstring">"ispis&nbsp;random&nbsp;vijesti<br />"</span><span class="syntaxkeyword">;<br />}<br />echo</span><span class="syntaxstring">"</div>"</span><span class="syntaxkeyword">;<br />unset(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$broj</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$b</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$d</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$f</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$g</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$h</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">mysql_free_result</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$citaj</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span class="syntaxdefault">$citaj</span><span class="syntaxkeyword">);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span class="syntaxdefault">$redak</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">?><br /></span></span>

Ovo nije rješenje tvog problema, već recimo ideja za unaprjeđenjem skripte.

Prvo, probaj ovakav upit u bazu

“SELECT * FROM bazetina ORDER BY RAND() LIMIT 8”

s kojim ćeš dobit 8 slučajnih (i svih 8 različitih) redaka. S random kako ti radiš možeš pogodit 2 (ili svih 8) ista ID-jeva. Nije velika vjerojatnost za to, ali postoji.

Drugo, u while petlji prvo imaš $i++, a zatim ga koristiš, pa tvoje polje ima za prvi element zapravo indeks 1, a ne 0 kako je uobičajeno za polja.

Uz gornji upit naravno ide i manja izmjena skripte, i na kraju štediš jedan upit u bazu jer možeš odmah ispisivat 8 slučajnih redaka (skripta će raditi brže, točnije i štediš memoriju).

Bravo, samim time sam se riješio svih polja koja mi više ni netrebaju.

A sad da baš od nekih par tisuća vijesti dobim 8 istih u jednom slučajnom odabiru zbilja je mala vjerojatnost, stoga baš i da se desi, (a šanse su vrlo male)nema veze ionako je samo prikaz malih thumbova.

Imaš rep+ :wink:

Drago mi je da mogu pomoći.

Sad se više ne bi trebalo nikad pojavit 2 puta ista vijest, osim ako je dva puta upisana u bazu ili MySQL ima neki bug.