Jump to content

Recommended Posts

To start, here is a snippet of my code...

    
/* Updates the news database */
function add_newsdb($filename, $news)
{
if(file_exists($filename)){
	$fh = fopen($filename, "r");
	$old_news = fread($fh, filesize($filename));
	fclose($fh); 
}
        
// Load old news into memory
$fh3 = fopen($filename, "w");
fwrite($fh3, "$news $old_news");
fclose($fh3);

}

if(strcmp($url, ''))	{
add_newsdb("news.dat", summary_page($subject, $date, $summary, $url));
}
else {
add_newsdb("news.dat", summary_page_nl($subject, $date, $summary));
}

add_article("../../news.html");

 

Now, I'm focusing on the conditional (if/else) statement. I want to do is determine if the $url string is null or not. If it's null, I want to update a database with the news story and no url. If the $url is not null, I want to update the database with story & the corresponding url. My problem is, the database gets updated twice when I use this conditional statement: first with the proper update and then with a blank update. Any idea why this blank update takes place and how to correct it? See my attachment for a visual of my problem.

 

What I've already tried: if(strlen($url, '')), if(strncmp($url, "http", 4)), etc with the same result. Also, I should mention that if I only include if(strcmp($url, '')) and no else{}, then there is only one update to the database.

 

Any help would be greatly appreciated.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/105091-solved-conditional-statement-help/
Share on other sites

Btown2, if($url != "") {}... was incorrect. I appreciate the quick reponse, but I'm curious if you read my entire post. After all, I specifically mentioned trying all types of string manipulation/comparrison/etc and nothing regarding the latter has yielded correct results. I think the problem is more dubious than we imagine. That being said, perhaps I should post the entire code I'm using:

 

/* Create a news story based on our template page */
function summary_page ($subject, $date, $summary, $url)
{
        global $summary_template;
        $t = new Template();
        $t->set_file("SummaryPage", $summary_template);
        $date = nl2br($date);
        $summary =  nl2br($summary);
        $t->set_var( array(
"subject" => $subject,
"date"    => $date,
"summary" => $summary,
"body" => $url));
        $t->parse("Summary", "SummaryPage");
        return $t->get_var("Summary");
        }

/* Create a news story based on our template page (NO HYPERLINK)*/
function summary_page_nl ($subject, $date, $summary)
{
        global $summary_template_nl;
        $t = new Template();
        $t->set_file("SummaryPageNl", $summary_template_nl);
        $date = nl2br($date);
        $summary =  nl2br($summary);
        $t->set_var( array(
"subject" => $subject,
"date"    => $date,
"summary" => $summary));
        $t->parse("SummaryNl", "SummaryPageNl");
        return $t->get_var("SummaryNl");
}

/* Updates the news database */
function add_newsdb($filename, $news)
{
        if(file_exists($filename)){
$fh = fopen($filename, "r");
$old_news = fread($fh, filesize($filename));
fclose($fh); 
        }
        // Load old news into memory
        $fh3 = fopen($filename, "w");
        fwrite($fh3, "$news $old_news");
        fclose($fh3);

}

/* Updates news.html */
function add_article($filename)
{

        // Load header into memory
        if (file_exists("header.dat")){
            $fh = fopen("header.dat", "r");
            $header = fread($fh, filesize("header.dat"));
            fclose($fh);
        }

        // Load footer into memory
        if (file_exists("footer.dat")){
            $fh2= fopen("footer.dat", "r");
            $footer = fread($fh2, filesize("footer.dat"));
            fclose($fh2);
        }

        // Load old news into memory
        if(file_exists("news.dat")){
$fh1 = fopen("news.dat", "r");
$old_news = fread($fh1, filesize("news.dat"));
fclose($fh1);
        }

        // Publish the News
        unlink("../../news.html");
        $fh = fopen($filename, "w");
        fwrite($fh, "$header \n $old_news \n\n $footer");
        fclose($fh);
}

if(strcmp($url, "")){
        add_newsdb("news.dat", summary_page($subject, $date, $summary, $url));
}
else {
        add_newsdb("news.dat", summary_page_nl($subject, $date, $summary));
}

add_article("../../news.html");

 

Again, help is greatly appreciated.

my first thing was wrong, do this

 


if($url != "")
{
     /* add your news*/
}
else
{
    /*add your news as null*/
}

 

Uhh.. wouldn't it be better to do this?

 

<?php
if(!empty($url))
{
     /* add your news*/
}
else
{
    /*add your news as null*/
}
?>

I solved the problem myself. Let's look at my original code:

 

<?php...
if(strcmp($url, "")){
        add_newsdb("news.dat", summary_page($subject, $date, $summary, $url));
}
else {
        add_newsdb("news.dat", summary_page_nl($subject, $date, $summary));
}
?>

 

The solution: When the page loaded, the php routine initialized and ran its course up to the "?>". Hence, when it checked for an empty string in the conditional statement, one was found and the database was updated with an empty entry. To counter-act this, I needed two if statements...like this:

 

<?php...
if(!empty($another_string)) {
     if(strcmp($url, "")){
         add_newsdb("news.dat", summary_page($subject, $date, $summary, $url));
     }
     else {
         add_newsdb("news.dat", summary_page_nl($subject, $date, $summary));
     }
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.