0

I have a php script which I'm using on my newest website, and while I've never had a problem with using this same type of coding before, I'm now getting a lot of errors. Here's the code that I'm using:

$articles_sql = "SELECT * FROM articles ORDER BY added DESC LIMIT 20";
$articles_res = mysqli_query($con, $articles_sql);
while($articles = mysqli_fetch_assoc($articles_res)){
    $articles_id = $articles["id"];
    $articles_title = $articles["title"];
    $articles_text = $articles["text"];
    $articles_text = strip_tags($articles_text);
    $articles_text = substr($articles_text,0,140);
    $articles_added = $articles["added"];

    $articles_year = date("Y", strtotime($articles_added));
    $articles_date = date("jS F Y, g:ia", strtotime($articles_added));

    $display_random_articles .= " // ERROR FOR THIS LINE

        <div class=\"block\">
            <div class=\"blockAvatar\">
                <img class=\"blockAvatarImg\" src=\"images/articles/$articles_year/$articles_id.jpg\" />
                <div class=\"blockAvatarCover\"></div>
            </div>
            <div class=\"blockIcons blockIconPaper\"></div>
            <div class=\"blockContent blockContentNarrow\">
                <li class=\"blockDate\">$articles_date</li>
                <li><strong><a href=\"article.php?id=$articles_id\">$articles_title</a></strong></li>
                <li class=\"blockText\">$articles_text...</li>
                <li class=\"blockDate\">... <a href=\"article.php?id=$articles_id\">Read On</a> &raquo;&raquo;</li>
            </div>
            <div class=\"clearLeft\"></div>
        </div>

    ";
}

Now everything appears to be fine visually but I've just looked at the Error_Log and it's full of the same little errors warning of undefined variables:

PHP Notice: Undefined variable: display_random_articles in ... On the line that I outlined above. Does anyone know why I'm getting this error and more importantly, is there any way of getting rid of it?

  • Well you got a funny error message! `display_random_blogs` <> `$display_random_articles` – Rizier123 Mar 30 '15 at 20:31
  • try to define $display_random_articles = ''. – Alive to die - Anant Mar 30 '15 at 20:32
  • Sorry @Rizier123, Like I said I'm getting a few errors so both `display_random_blogs` & `display_random_articles` are showing up – Foot Promoter Mar 30 '15 at 20:34
  • @anantkumarsingh, Sorry but how do I do that? – Foot Promoter Mar 30 '15 at 20:34
  • You never bothered initializating `$display_random_articles`, so when the `$display_random_articles .= ".etc..."` executes for the first time, there's no variable to concatenate with, hence your error. – Marc B Mar 30 '15 at 20:36
  • Try to write it very upside before while – Alive to die - Anant Mar 30 '15 at 20:38
  • I'm sorry @MarcB but I don't understand! How do I initialize? – Foot Promoter Mar 30 '15 at 20:38
  • `$d_r_a = ''` somewhere before you start the loop, basically. just give it a value. any value. – Marc B Mar 30 '15 at 20:49
  • @MarcB - So why won't THIS variable get an error? I thought that the value of the `$d_r_a` was what is written after the `=`! Somewhere else in my script I have `$site_url = "http://.....com"` and this doesn't get any errors - What's different about my `$r_d_a`? – Foot Promoter Mar 30 '15 at 20:54
  • @MarcB Is it something to do with variables that have a `.` placed before the `=`? – Foot Promoter Mar 30 '15 at 20:56
  • because you're using `.=`. that's shortcut for `$x = $x . $y`. you have to ACCESS $x to concatenate it with $y, before the result can be assigned back to it. since it doesn't exist on the first loop iteration, you're effectively doing `$x = undefined . $y`. – Marc B Mar 30 '15 at 20:57
  • Yeah I looked back at my Error Log and worked out that variables with a `.=` were where the errors were showing up. I'll go back and initialize each variable above this scripting. Cheers @MarcB – Foot Promoter Mar 30 '15 at 21:01

0 Answers0