简体   繁体   中英

Code causing page to render blank with PHP

So this is the first PHP script (if it's even called that?) that I've ever written from scratch, and I'm having an issue in that when it's applied to the existing (and otherwise working) page, the page shows up blank. I was hoping one of the many people who are better and more experienced than I am can take a look and find what is no doubt a blatant syntax error. Thank you in advance to anyone that shows me the light!

<?php
$sql = "SELECT * FROM 'jos_downloads_files'";
$rows = $db->fetch_all_array($sql);

foreach($rows as $row) {
    $filename = $row['filetitle'];
    $filepath = $row['realname'];
    $featured = $row['featured'];
    $id = $row['containerid'];
}

foreach ($id as $containername) {

    if ($id == 2) {
        $containername ="Incidental Information";
    }

    if ($id == 3) {
        $containername ="Monitoring Reports";
    }

    if ($id == 4) {
        $containername ="Agendas";
    }

    if ($id == 5) {
        $containername ="Decision Prep";
    }

    if ($id == 6) {
        $containername ="Agendas";
    }

    if ($id == 7) {
        $containername ="Policy Governance";
    }

    echo '<div class = "moduletable">
        <h3>' . $containername . '</h3>'; 

    foreach ($featured as $featureedtrue) {

        if ($featuredtrue == 1) {
            echo '<ul class="weblinks">
                <li>
                    <a>' . $filename . '</a>
                </li>';
        }
    }
}   
?>

As celeriko mentioned, you never declared $db prior to using it. As Xatenev mentioned, I'm not sure what fetch_all_array is, perhaps fetchAll() ? Finally, as Valery Statichny mentioned, $id will never be an array.

In the future, it is helpful to turn on error reporting so that you can see where your scripts are crashing. You can do so by adding the following:

ini_set('display_errors',1); 
error_reporting(E_ALL);

In production environments, turn error reporting off so that users don't see error messages.

Edit

More from Xatenev:

Why are you looping foreach $featured? $featured can only be ONE entry at the moment. You have to write $featured[] = $row['featured']; to get an array where u can loop through. Same to $id. 5. You are looping through $featured but use $filename then? $filename will print an array EVERY TIME you loop through $featured. So for example when you have 100 entries in $featured, you have 100x the same content when you print out $filename. – Xatenev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM