Avatar billede svjensen Nybegynder
24. maj 2004 - 20:59 Der er 2 kommentarer og
1 løsning

MySQL udtræk driller.

Ved hjælp af nedenstående henter jeg indholdet af en database, som derefter gerne skulle vises. Jeg kan bare ikke få loopet til at virke. Den skriver kun første row, men til gengæld kommer den aldrig ud af loopet.

Hvad er der galt?

echo "<h1 align=center>Galleri</h1>";
echo "<p align=center>Vælg galleri:<br><br>";
$strSQL = "SELECT * FROM gallerier";
$res = mysql_query($strSQL);

echo "<table align=center border=0><tr>";
$nummer = 1;

while ($row = mysql_fetch_array($res)) {
    $strSQLAntal = "SELECT COUNT(*) AS antal FROM galleripics WHERE galleriid = ".mysql_result($res,0,"galleriid");
    $resAntal = mysql_query($strSQLAntal);
    echo"<td><a href='galleri.php?action=show&id=".mysql_result($res,0,"galleriid")."'>".mysql_result($res,0,"galleri");
    echo " (".mysql_result($resAntal,0,"antal").")</a></td>";
    echo "<td>&nbsp;</td>";
    If ($nummer == 4){
        echo "</tr><tr>";
        $nummer = 0;
    }
    $nummer = $nummer + 1;
}
echo "</table>";
Avatar billede dennismp Nybegynder
24. maj 2004 - 21:16 #1
mysql_result($res,0,"galleriid");

vil du altid printe det samme ud ? Du får jo række 0 hver gang?

Sådan her vil jeg kode det samme;
<?php
echo "<h1 align=center>Galleri</h1>";
echo "<p align=center>Vælg galleri:<br><br>";
$strSQL = "SELECT * FROM gallerier";
$res = mysql_query($strSQL);

echo "<table align=center border=0><tr>";
$nummer = 1;

while ($row = mysql_fetch_array($res)) {
    $strSQLAntal = "SELECT COUNT(*) AS antal FROM galleripics WHERE galleriid = {$row['galleriid']}";
    $resAntal = mysql_query($strSQLAntal);
    $rowAntal = mysql_fetch_array($res);
    echo"<td><a href='galleri.php?action=show&id={$row['galleriid']}'>".$row['galleri'];
    echo " (". $rowAntal['antal'].")</a></td>";
    echo "<td>&nbsp;</td>";
    If ($nummer == 4){
        echo "</tr><tr>";
        $nummer = 0;
    }
    $nummer = $nummer + 1;
}
echo "</table>";
?>
Avatar billede coderdk Praktikant
24. maj 2004 - 21:42 #2
Og hvis du altid tilgår felterne med $row['feltnavn'] så brug mysql_fetch_assoc i stedet for mysql_fetch_array
Avatar billede svjensen Nybegynder
25. maj 2004 - 17:21 #3
Jeg har nu prøvet at indsætte forslaget i scriptet, men der kommer en underlig fejl:

Parse error: parse error, unexpected $ in /customers/vinther-jensen.dk/vinther-jensen.dk/httpd.www/galleri.php on line 214

Det underlige er, at der ikke er noget $-tegn i linie 214.

Den fulde fil kommer her:
*******************************************
<?
include("top.php");
include("include/function-popup_picture.php");
?>

<center>
<?
if ($_GET['action'] == "show"){

    $strSQLGalleri = "SELECT * FROM gallerier WHERE galleriid = ".$_GET['id'];
    $resGalleri = mysql_query($strSQLGalleri);
   
    $strSQL = "SELECT * FROM galleripics WHERE galleriid = ".$_GET['id'];
    $res = mysql_query($strSQL);
   
    echo "<h1>".mysql_result($resGalleri,0,"galleri")."</h1>";
    echo mysql_result($resGalleri,0,"tekst");
    echo "<table align=center>";
    echo "<tr>";
    $nummer = 1;
    while ($row = mysql_fetch_assoc($res)) {
        echo"<td class=TDCenter>";
        $billede = "thumb_".mysql_result($res,0,"billed");
        if (mysql_result($res,0,"tekst") <> ""){?>
            <a href="java script:void(0);" onmouseover="return overlib('<?echo mysql_result($res,0,"tekst")?>');" onclick="PopupBillede('gallery/<?echo mysql_result($res,0,"billed")?>', '<?echo mysql_result($res,0,"tekst")?>', '<?echo mysql_result($res,0,"width")?>', '<?echo mysql_result($res,0,"height")?>');return false;" onmouseout="return nd();">
            <img src="gallery/thumbs/<?echo $billede ?>" border=0></a>
    <?    }else{?>
            <a href="java script:void(0);" onclick="PopupBillede('gallery/<?echo mysql_result($res,0,"billed")?>', '<?echo mysql_result($res,0,"tekst")?>', '<?echo mysql_result($res,0,"width")?>', '<?echo mysql_result($res,0,"height")?>');return false;">
            <img src="gallery/thumbs/<?echo $billede ?>" border=0></a>
    <?    }
        if ($_SESSION['admin'] == true){
            echo "<br><a href='galleri.asp?action=sletpic&id=".mysql_result($res,0,"uid")."'>Slet</a>";
        }
        echo "</td>";
        If ($nummer == 6){
            echo "</tr><tr>";
            $nummer = 0;
        }
        $nummer = $nummer + 1;
    }

    echo "</tr></table>";

}else if ($_GET['action'] == "sletpic"){

    if ($_SESSION['login'] == true){
        $strSQL = "SELECT billed FROM galleripics WHERE uid = ".$_GET['id'];
        //set rs = conn.execute(strSQL)

        //'opretter et FileSystemObject
        //Set fso = CreateObject("Scripting.FileSystemObject")

        //'Sletter filen + thumb
        //fso.deletefile("gallery/" & rs("billed"))
        //fso.deletefile("gallery/thumbs/thumb_" & rs("billed"))

        //'For en god ordens skyld fjerner vi fso fra rammene
        //set fso = nothing

        $strSQL = "DELETE FROM galleripics WHERE uid = ".$_GET['id'];
        //set rs = conn.execute(strSQL)

        //response.redirect("galleri.asp?action=show&id=" & request.querystring("id"))
    }
}else if ($_GET['action'] == "nytgalleri") {
    if ($_SESSION['login'] == true){
        $strSQL = "SELECT COUNT(*) AS antal FROM gallerier WHERE galleri = '".$_POST['txtgalleri']."'";
        $res = mysql_query($strSQL);

        if (mysql_result($res,0,"antal") == "0"){
            $strSQL = "INSERT INTO gallerier (galleri, tekst) VALUES ('".$_POST['txtgalleri']."', '";
            $strSQL = $strSQL.$_POST['txttekst']."')";
            $res = mysql_query($strSQL);
        }else{?>
            <script language="javascript">
                alert('Galleriet eksisterer allerede.');
            </script>
    <?    }
        header("location: galleri.php");
    }
}else if ($_GET['action'] == "database"){
    if ($_SESSION['admin'] == "JA"){
        $strSQL = "INSERT INTO galleripics (galleriid, billed, tekst) ";
        $strSQL = $strSQL."VALUES ('".$_POST['ddgalleri']."', ";
        $strSQL = $strSQL."'".$_POST['txtfilnavn']."', ";
        $strSQL = $strSQL."'".$_POST['txttekst']."') ";
        //set rs = conn.execute(strSQL)
   
        header("location: galleri.php");
    }
}else if ($_GET['action'] == "updategalleri"){
    if ($_SESSION['login'] == "ja"){
        $strSQL = "UPDATE gallerier SET ";
        $strSQL = $strSQL."galleri = '".$_POST['txttitel']."', ";
        $strSQL = $strSQL."tekst = '".$_POST['txttekst']."' ";
        $strSQL = $strSQL."WHERE galleriid = ".$_POST['txtnummer'];
        //set rs = conn.execute(strSQL)

        //header("location: galleri.php");
    }
}else if($_GET['action'] == "redigergalleri"){
    if ($_SESSION['login'] == "ja"){
        $strSQL = "SELECT * FROM gallerier WHERE galleriid = " & request.form("ddgalleri");
        //set rs = conn.execute(strSQL)%>
        ?>
        <form action="galleri.php?action=updategalleri" method=post>
        <input type=hidden name=txtnummer value="<%= rs("galleriid")%>">
        <table border=1>
        <tr><th>Titel:</th><td><input type=text name=txttitel size=25 value="<%= rs("galleri")%>"></td></tr>
        <tr><th>Beskrivelse:</th><td><input type=text name=txttekst size=25 value="<%= rs("tekst")%>"></td></tr>
        <tr><td colspan=2><input type=submit value=Opdater></td></tr>
        </table>
        </form>
<?    }
}else if ($_GET['action'] == "sletgalleri"){
    if ($_SESSION['login'] == true){

        $galleri = request.form("ddgalleri");
        $strSQL = "SELECT * FROM galleripics WHERE galleriid = '".$galleri."'";
        //set rs = conn.execute(strSQL)

//        While not rs.EOF

//            Set fso = CreateObject("Scripting.FileSystemObject")
//            fso.deletefile("gallery/" & rs("billed"))
//            fso.deletefile("gallery/thumbs/thumb_" & rs("billed"))
//            set fso = nothing
//            rs.MoveNext
//        Wend

//        strSQLSlet = "DELETE FROM galleripics WHERE galleriid = '" & galleri & "'"
//        set rsSlet = conn.execute(strSQLSlet)

//        strSQLSletGalleri = "DELETE FROM gallerier WHERE galleriid = '" & galleri & "'"
//        set rsSletGalleri = conn.execute(strSQLSletGalleri)

//        response.redirect("galleri.asp")
//    }
}else{
    echo "<h1 align=center>Galleri</h1>";
    echo "<p align=center>Vælg galleri:<br><br>";
    $strSQL = "SELECT * FROM gallerier";
    $res = mysql_query($strSQL);

    echo "<table align=center border=0><tr>";
    $nummer = 1;

    while ($row = mysql_fetch_array($res)) {
        $strSQLAntal = "SELECT COUNT(*) AS antal FROM galleripics WHERE galleriid = ".$row['galleriid'];
        $resAntal = mysql_query($strSQLAntal);
        $rowAntal = mysql_fetch_array($resAntal);
        echo"<td><a href='galleri.php?action=show&id=".$row['galleriid']."'>".$row['galleri']." (".$rowAntal['antal'].")</a></td>";
        echo "<td>&nbsp;</td>";
        If ($nummer == 4){
            echo "</tr><tr>";
            $nummer = 0;
        }
        $nummer = $nummer + 1;
        echo "</table>";
    }

if ($_SESSION['admin'] == true){
    echo"<hr>";
?>
    <table border=0 width=95%>
    <tr><th>Opret nyt galleri</th><th>Rediger galleri</th><th>Slet galleri</th></tr>
    <tr>
        <td    class=TDCenter>
            <form action="galleri.php?action=nytgalleri" method=post>
            <table border=0>
            <tr><td class=TDRight>Titel:</td><td><input type=text size=25 name=txtgalleri></td></tr>
            <tr><td class=TDRight>Beskrivelse:</td><td><input type=text size=25 name=txttekst></td></tr>
            <tr><td colspan=2 class=TDCenter><input type=submit value="Opret"></td></tr>
            </table>
        </form>
        </td>

        <td class=TDCenter>
        <form action="galleri.php?action=redigergalleri" method=post>
<?        $strSQLGallerier = "SELECT * FROM gallerier";
        $resGallerier = mysql_query($strSQLGallerier);
        echo "<select size=1 name=ddgalleri>";
        echo "<option value=''></option>";
        while ($rowGallerier = mysql_fetch_assoc($resGallerier)) {
            echo "<option value='".$rowGallerier['galleriid']."'>".$rowGallerier['galleri']."</option>";
        }
        echo "</select>";
?>        <br><br><input type=submit value="Rediger">
        </form>

        </td>

        <td class=TDCenter>
        <form action="galleri.php?action=sletgalleri" method=post onsubmit="return confirm('Er du sikker på, at du vil slette hele galleriet?');">
<?        $strSQLGallerier1 = "SELECT * FROM gallerier";
        $resGallerier1 = mysql_query($strSQLGallerier1);
        echo "<select size=1 name=ddgalleri>";
        echo "<option value=''></option>";
        while ($rowGallerier1 = mysql_fetch_assoc($resGallerier1)) {
            echo "<option value='".$rowGallerier1['galleriid']."'>".$rowGallerier1['galleri']."</option>";
        }
        echo "</select>";

?>        <br><br><input type=submit value="Slet">
        </form>
        </td>
    </tr>
    </table>
<?}
if ($_SESSION['login'] == true){
    echo "<a href='galleriupload.php'>Upload billeder</a>";
}?>
</body>
</html>
***************************
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester