Jump to content

[SOLVED] Function Probleme


SpireLink

Recommended Posts

Hi again,

 

I am making a site that will show data accourding to the type of data, I am passing the type with the link

 

sample links

show.php?do=comany&id=  (to show company data)

show.php?do=news&id= (to show news data)

show.php?do=sponsor&id= (to show sponsor data)

show.php?do=event&id= (to show event data)

 

I would like to use one file insted of making 4 files, notice that each type have its own table,

i have tried to make a file news.php and it containes this code

 

<?php
include("includes/config.php");
$news = trim($HTTP_GET_VARS[id]);
$query_news_rs = "SELECT * FROM news WHERE id=$news";
$news_rs = mysql_query($query_news_rs, $db) or die(mysql_error());
$row_news_rs = mysql_fetch_assoc($news_rs);

// *** Get News Details *** 

$id=$row_news_rs['id'];
$channel_id=$row_news_rs['channel_id'];
$news_title=$row_news_rs['news_title'];
$news_url=$row_news_rs['news_url'];
$news_pic=$row_news_rs['news_pic'];
$news_date=$row_news_rs['news_date'];
$news_content=$row_news_rs['news_content'];
$news_display=$row_news_rs['news_display'];

echo "
<TABLE WIDTH='100%' id='News Details'>
<TR>
<TD>
$id
<BR/>
$channel_id
<BR/>
$news_title
<BR/>
$news_url
<BR/>
$news_pic
<BR/>
$news_date
<BR/>
$news_content
<BR/>
$news_display
<BR/>
</TD>
</TR>
</TABLE>";
// *** Free Database ***
mysql_free_result($news_rs);
?>

 

now in show.php i have this code

 

<?php
include("includes/config.php");
$id= trim($HTTP_GET_VARS[id]);

// function company {
// company actions
// }
function news ($id)
{
$query_news_rs = "SELECT * FROM news WHERE id=$id";
$news_rs = mysql_query($query_news_rs, $db) or die(mysql_error());
$row_news_rs = mysql_fetch_assoc($news_rs);

// *** Get News Details *** 

$id=$row_news_rs['id'];
$channel_id=$row_news_rs['channel_id'];
$news_title=$row_news_rs['news_title'];
$news_url=$row_news_rs['news_url'];
$news_pic=$row_news_rs['news_pic'];
$news_date=$row_news_rs['news_date'];
$news_content=$row_news_rs['news_content'];
$news_display=$row_news_rs['news_display'];

echo "
<TABLE WIDTH='100%' id='News Details'>
<TR>
<TD>
$id
<BR/>
$channel_id
<BR/>
$news_title
<BR/>
$news_url
<BR/>
$news_pic
<BR/>
$news_date
<BR/>
$news_content
<BR/>
$news_display
<BR/>
</TD>
</TR>
</TABLE>";
// *** Free Database ***
mysql_free_result($news_rs);
}
// function sponsor {
// sponsor actions
// }
// function event {
// event actions
// }
?>

 

when I use the link show.php?do=news&id=1 to show the data of id 1 it show me a blank page,

please correct me, notice that when i use news.php it shows me the data as i want, but not in show.php

Link to comment
https://forums.phpfreaks.com/topic/63328-solved-function-probleme/
Share on other sites

You need call the news() function in shows.php. PHP wont run that function until you tell it to. PHP wont call functions automatically.

<?php
include 'includes/config.php';

// make sure do isset
if(!isset($_GET['do']))
{
    die('No action found');
}

// make sure id is set and that is of a numrical value
if(!isset($_GET['id']) || !is_numerical($_GET['id']))
{
    die('id not found');
}

// call the function
switch($_GET['do'])
{
    case 'company':
    case 'news':
    case 'sponsor':
    case 'event':

        // get the id
        $id = $_GET['id'];

        // call the function based _GET['do'] variables value
        $_GET['do']($id);
    break;
}

// function company {
// company actions
// }
function news ($id)
{
    $query_news_rs = "SELECT * FROM news WHERE id=$id";
    $news_rs = mysql_query($query_news_rs, $db) or die(mysql_error());
    $row_news_rs = mysql_fetch_assoc($news_rs);

    // *** Get News Details ***
    $id           = $row_news_rs['id'];
    $channel_id   = $row_news_rs['channel_id'];
    $news_title   = $row_news_rs['news_title'];
    $news_url     = $row_news_rs['news_url'];
    $news_pic     = $row_news_rs['news_pic'];
    $news_date    = $row_news_rs['news_date'];
    $news_content = $row_news_rs['news_content'];
    $news_display = $row_news_rs['news_display'];

    // HEREDOC SYNTAX (http://php.net/heredoc)
    echo <<<EOF
<TABLE WIDTH="100%" id="News Details">
  <TR>
    <TD>
      $id<BR/>
      $channel_id<BR/>
      $news_title<BR/>
      $news_url<BR/>
      $news_pic<BR/>
      $news_date<BR/>
      $news_content<BR/>
      $news_display<BR/>
    </TD>
  </TR>
</TABLE>
EOF;
// DONOT MOVE OR PLACE ANYTHING ON THE LINE ABOVE

     // *** Free Database ***
    mysql_free_result($news_rs);
}

// function sponsor {
// sponsor actions
// }
// function event {
// event actions
// }
?>

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.