16. maj 2004 - 14:06
Der er
9 kommentarer og 1 løsning
Udtræk af DB
Jeg har en 'opslagstavle' som er gemt i en DB. Udtræk af DB skrives i tabel således: Ex1. <?php while(bal bla bla) { ?> <tr> <td> <table> <tr> <td><?php $udtræk ?></td> </tr> </table> </td> </tr> <?php } ?> .... og opslagene skrives pænt under hinnanden. Hvis jeg nu vil lave min HTML opbygning om til: Ex2. <?php while(bal bla bla) { ?> <tr> <td> <table> <tr> <td><?php $udtræk ?></td> <td><?php $udtræk?></td> </tr> </table> </td> </tr> <?php } ?> ... og så fremdeles hvordan for jeg så et opslag i den første <td> et andet udtræk i den anden <td> osv.?
Annonceindlæg tema
Cloud & AI
I dette særtema ser vi på, hvordan cloud og AI bliver fundamentet for virksomhedernes digitale forretning, og hvordan de nye muligheder for automatisering og forretningsværdi kan udnyttes uden at miste overblik, sikkerhed og menneskelig kontrol.
Hvis du f.eks. bruger MySQL, vil det se således ud: <? while($row = mysql_fetch_array($query)) { echo "<tr><td><table><tr>"; echo "<td>".$row["fieldname"]."</td>"; if($row = mysql_fetch_array($query)) { echo "<td>".$row["fieldname"]."</td>"; } else { echo "</td> </td>"; } echo "</tr></table></td></tr>"; } ?> Jeg har ikke lige testet koden, men ideen burde være god nok. A.
Hm... hvordan kan det vise 2 forskellige opslag? Det viser, så vidt jeg lige kan få det til, blot det samme.
linien: $row = mysql_fetch_array($query) gør at du henter næste post i søgningen, derfor for du næste post vist inde i if-løkken hvis posten eksisterer, eller skriver den <td> </td> PS: Så lige en mindre fejl i ovenstående: echo "</td> </td>"; rettes til: echo "<td> </td>";
Det virker. Tusind tak for hjælpen :0)
hm, prøv at poste koden for siden (husk at fjerne passwords osv.) :) A.
<?php while($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { $id = $resultat_dlkkurser["id"]; $overskrift = $resultat_opslag["overskrift"]; $kontakt = $resultat_opslag["kontakt"]; $tekst = nl2br($resultat_opslag["tekst"]); $dato = $resultat_opslag["dato"]; ?> <tr> <td valign="top"> <table style="width:270;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:270; padding-bottom:20px;" valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> <td> <table style="width:30px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:30px; height:30px;"><img src="image/stift1.gif" border="0"></td> </tr> </table> </td> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td valign="top"> <table style="width:268px;" cellspacing="0" cellpadding="0" border="0" class="border2 gul"> <tr> <td style="width:268; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Overskrift:</span> <?php echo $overskrift ?></td> </tr> <tr> <td style="width:268; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Kontakt:</span> <?php echo $kontakt ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:5px" class="p1" align="right"><?php echo $dato ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:11px; padding-bottom:11px; padding-left:11px;"> <table style="width:246px;" cellspacing="0" cellpadding="0" border="0" class="papir"> <tr> <td style="width:246;" class="p1"><?php echo $tekst ?></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> <td style="width:20px; padding-bottom:20px;"> </td> <?php if($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { ?> <td valign="top"> <table style="width:270;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:270; padding-bottom:20px;" valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> <td> <table style="width:30px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:30px; height:30px;" valign="bottom"><img src="image/stift2.gif" border="0"></td> </tr> </table> </td> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0" class="border2 gul"> <tr> <td style="width:270; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Overskrift:</span> mangler dit barn en cykel?</td> </tr> <tr> <td style="width:270; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Kontakt:</span> Tlf: 12345678</td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:5px" class="p1" align="right">12/3 2004</td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:11px; padding-bottom:11px; padding-left:11px;"> <table style="width:246px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:246;" class="p1 papir"> Cykel sælges. Størrelse 12år. Sort og guld. Pris ca. 500 kr. Ring og få en prøvetur! </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> <?php }else{ ?> <td> </td> <?php } ?> </tr> <?php } ?>
Hov... fejl .. dette er den rigtige <?php while($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { $id = $resultat_dlkkurser["id"]; $overskrift = $resultat_opslag["overskrift"]; $kontakt = $resultat_opslag["kontakt"]; $tekst = nl2br($resultat_opslag["tekst"]); $dato = $resultat_opslag["dato"]; ?> <tr> <td valign="top"> <table style="width:270;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:270; padding-bottom:20px;" valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> <td> <table style="width:30px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:30px; height:30px;"><img src="image/stift1.gif" border="0"></td> </tr> </table> </td> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td valign="top"> <table style="width:268px;" cellspacing="0" cellpadding="0" border="0" class="border2 gul"> <tr> <td style="width:268; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Overskrift:</span> <?php echo $overskrift ?></td> </tr> <tr> <td style="width:268; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Kontakt:</span> <?php echo $kontakt ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:5px" class="p1" align="right"><?php echo $dato ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:11px; padding-bottom:11px; padding-left:11px;"> <table style="width:246px;" cellspacing="0" cellpadding="0" border="0" class="papir"> <tr> <td style="width:246;" class="p1"><?php echo $tekst ?></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> <td style="width:20px; padding-bottom:20px;"> </td> <?php if($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { ?> <td valign="top"> <table style="width:270;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:270; padding-bottom:20px;" valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> <td> <table style="width:30px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:30px; height:30px;" valign="bottom"><img src="image/stift2.gif" border="0"></td> </tr> </table> </td> <td> <table style="width:120px; height:30px" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:120px; height:20px;" class="border10"> </td> </tr> <tr> <td style="width:120px; height:10px; filter: alpha(opacity=70);" class="moerk"></td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td valign="top"> <table style="width:270px;" cellspacing="0" cellpadding="0" border="0" class="border2 gul"> <tr> <td style="width:270; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Overskrift:</span> <?php echo $overskrift ?></td> </tr> <tr> <td style="width:270; height:18px; padding-left:5px" align="left" valign="middle" class="border10 p1"><span class="p9">Kontakt:</span> <?php echo $kontakt ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:5px" class="p1" align="right"><?php echo $dato ?></td> </tr> <tr> <td style="width:268; padding-top:3px; padding-right:11px; padding-bottom:11px; padding-left:11px;"> <table style="width:246px;" cellspacing="0" cellpadding="0" border="0"> <tr> <td style="width:246;" class="p1 papir"><?php echo $tekst ?></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> <?php }else{ ?> <td> </td> <?php } ?> </tr> <?php } ?>
ok, har den nu :) I starten af koden ved din while løkke har du: while($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { $id = $resultat_dlkkurser["id"]; $overskrift = $resultat_opslag["overskrift"]; $kontakt = $resultat_opslag["kontakt"]; $tekst = nl2br($resultat_opslag["tekst"]); $dato = $resultat_opslag["dato"]; og du benytter så de variable senere hen. I din if-lykke har du bare: if($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { Det betyder at $resultat_opslag bliver sat rigtigt nok, men at du ikke indlæser værdierne i de variable du rent faktisk bruger, og derfor får du samme post 2 gange. Prøv at ændre if-løkken til: if($resultat_opslag = mysql_fetch_array($resultat_hent_opslag)) { $id = $resultat_dlkkurser["id"]; $overskrift = $resultat_opslag["overskrift"]; $kontakt = $resultat_opslag["kontakt"]; $tekst = nl2br($resultat_opslag["tekst"]); $dato = $resultat_opslag["dato"]; A.
Selvf. Det kan jeg da se en god logik i... Tak igen du !
Vi tilbyder markedets bedste kurser inden for webudvikling