Jump to content

Simple customized function


ballouta

Recommended Posts

Hello

I am trying to create a simple update function.

but it is giving me syntax error.

May you please review my code

 

The function call:

	function update_article_stats ($article_num)

The Function Declaration in the functions.php

	function update_article_stats ($article_num)
	{ 
$query = "UPDATE `articles_content` SET `cnt` = cnt+1 WHERE `num` = '$article_num' ";
$result = mysql_query($query);
	}

 

Thank you so much

Link to comment
https://forums.phpfreaks.com/topic/222686-simple-customized-function/
Share on other sites

Thanks the syntax error is gone,

in the functions.php

I have include ("global.inc.php");

and global.inc.php is the same directory with functions.php

 

The code is giving me a warning message, but I can see in the DB that the update is working (increasing number)

why is this warning

 

Warning: include(global.inc.php) [function.include]: failed to open stream: No such file or directory in /home/ahlussu/public_html/www.website.info/inc/functions.php on line 3

 

Thank you

in the showarticle.php I have:

	$article_num = $_GET['cid']; 
  //check if the article number exists, function call
check_artnum ($makalah_num);

 

//the function declaration


function check_artnum ($article_num) {
$query = mysql_query ("Select * FROM `articles_content` where `num` = '$article_num' ");
$num_rows = mysql_num_rows($query);
	if ($num_rows == 0 || $num_rows < 0) {
		return $article_num = '101';}
	else {}
}

 

is the logic and sytanx correct?

I tried ti write a wrong article number in the URL  it gave me a blank page.

 

Thank you

in the showarticle.php I have:

	$article_num = $_GET['cid']; 
  //check if the article number exists, function call
check_artnum ($makalah_num);

 

//the function declaration


function check_artnum ($article_num) {
$query = mysql_query ("Select * FROM `articles_content` where `num` = '$article_num' ");
$num_rows = mysql_num_rows($query);
	if ($num_rows == 0 || $num_rows < 0) {
		return $article_num = '101';}
	else {}
}

 

is the logic and sytanx correct?

I tried ti write a wrong article number in the URL  it gave me a blank page.

 

Thank you

 

This code doesn't actually output to the page, so hence the blank page. Personally I would do this, though I haven't checked that it works:

 

function check_artnum ($article_num) {
$query = mysql_query("Select * FROM `articles_content` where `num` = '".$article_num."'");
$num_rows = mysql_num_rows($query);
return ($num_rows == 0) ? '101' : $article_num;
}

 

echo check_artnum($_GET['cid']);

@ PHPTOM: Thank you so much, i like they way you code, u're prof.

 

Maybe I should have explained this for you before.

The variable $article_num is used in the show_article.php page several times, sometimes to get the article index titles, the body, and other attached or related articles.

 

one problem, if i enter 105 for article number in the page URL, the article number (e,.g 105) is wrong and should be fixed by this function and set it to 101 as default value.

 

How do I keep the correct $article_num value available for other queries and functions?

 

Thank you so much

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.