Forum Moderators: coopster

Message Too Old, No Replies

2 PHP Scripts on one page not working

         

Webmattster

6:51 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



I have a page with two small PHP scripts for a news site. I will post the code for the page below. Both scripts work fine when they are switched and when they are alone, but when they're together on one page they don't work.

<div id="cols">
<div class="cols_body">
<div class="cols_head">Spring Sports</div>
<div class="cols_body_sport">Boy's Basketball</div>
<div class="cols_body_headlines"><?php
$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "1";
$lim = "3";
$page = "/site/news/news.php";
include($tpath . "headlines.php");
?></div>
<div class="cols_body_sport">Girl's Soccer</div>
<div class="cols_body_headlines">Headlines about Sport</div>
<div class="cols_body_sport">Girl's Softball </div>
<div class="cols_body_headlines">Headlines about Sport</div>
<div class="cols_body_sport">Boy's Tennis </div>
<div class="cols_body_headlines"><?php
$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "2";
$lim = "3";
$page = "/index.php";
include($tpath . "headlines.php");
?></div>
<div class="cols_body_sport">Boy's Volleyball </div>
<div class="cols_body_headlines"></div>
<div class="cols_body_sport">Track</div>
<div class="cols_body_headlines">Headlines about Sport</div>
</div>

rj87uk

6:56 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know loads about PHP however I would think it is because you giving your variables two values.

Try changing the variables here:

<div class="cols_body_headlines"><?php
$tpath2 = "/Applications/MAMP/htdocs/site/news/";
$c2 = "2";
$lim2 = "3";
$page2 = "/index.php";
include($tpath2 . "headlines.php");
?></div>

Or something to that effect?

Webmattster

7:06 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



I just tried that and still the same thing happened. I'm going to keep trying things, but anymore help would be appreciated!

Thanks!

jatar_k

7:08 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Webmattster

could you describe how exactly their behaviour changes when both are there?

I would go with rj87uk and say variable contamination but maybe if you could describe it then we could better understand

Webmattster

7:15 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



When I put in the first one, closest to the top of the page, that one shows fine. When the second one is in there in addition to the first one, it's just blank where it would be. There are no errors of any type. The first one shows up still, but the 2nd one is blank.

Can you just change the variables and add numbers? I added the number 2 at the end of the variables in the 2nd one and that didn't work.

Thanks

jatar_k

7:17 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that would probably mean you would have to change the script as it won't be looking for the other variable names

I thought maybe it was an include_once type scenario so it just isn't being included the second time

hmm, if it's totally blank, I am guessing it isn't actually including it, not sure why

can you add a straight text echo to see if it comes out at all? do you see anything in the html source that shows it might be there? maybe an html error? (done that many times myself)

are you showing errors? no errors?

Webmattster

9:35 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



I'm getting no errors. It just acts like it's not even there. Is there any rule that you can't have to PHP scripts on the same page that do almost the same thing? I've tried an unset() but I didn't really know how to use it, and that didn't work either.

jatar_k

9:55 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> is there any rule that you can't have to PHP scripts

no, but you're including the same script twice, exactly the same

when I look at your two chunks of code

$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "1";
$lim = "3";
$page = "/site/news/news.php";
include($tpath . "headlines.php");

and

$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "2";
$lim = "3";
$page = "/index.php";
include($tpath . "headlines.php");

the only difference is the $c, so you could just do this for the first

$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "1";
$lim = "3";
$page = "/site/news/news.php";
include($tpath . "headlines.php");

and then this for the second

$c = "2";
include($tpath . "headlines.php");

could you send a couple vars, include it once and pull the content to a variable and then chop it up and output it wherever you need it?

I still don't really get what it's trying to do which makes it hard to think around

headlines makes me think you are getting some articles from somewhere, one chunk and then the next chunk, is that close?

Webmattster

10:28 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



I'm working on a high school athletic website. We are using a news script that generates the little snippets of code. Each sport has its own category in the news software and we are trying to make a page that has all of the sports headlines seperated by each sport with a background and title between the list of headlines for each. Does that make sense?

What do you mean by send some vars? One of the other people I work with had that type of idea also, but I have no idea how to do that.

jatar_k

10:33 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that makes sense

my thought then

make a function that returns what you need, you can include the file that contains the function and call the function as many times as you like. This would be much better than including the file every time.

how long is the code? you could paste it here and we could help make a function out of it.

Webmattster

10:35 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Which code would you like? The original code I posted was the includes page with the spring sports. Would you like to homepage also, which will pull the spring sports include onto it. The spring sports include has the generated code in it for each of the sports.

Webmattster

10:48 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Homepage:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/style2.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/menu/chromestyle.css" />
<script type="text/javascript" src="javascript/chrome.js"></script>
</head>

<body>
<div id="container">
<?php include("includes/header.php");?>
<div id="main">
<div id="leftcol">
<div id="topstory">
<div class="ts_header">Header</div>
<div class="ts_photo"><img src="images/headlines/empty.gif" /></div>
<div class="ts_photo_caption">About the photo</div>
<div class="otherstory">2 more headlines</div></div>
<?php include("includes/seasons/spring.php");?>
</div></div>
<div id="rightcol">
<div class="scoreboard"></div>
<div id="right_news1"><h1>Latest News Headlines<div class="rss"></div></h1>
<div id="right_news2">Headlines Links (bulleted lists) </div>
</div></div>
</body>

spring.php Includes (has the PHP script I'm having trouble with):
<div id="cols">
<div class="cols_body">
<div class="cols_head">Spring Sports</div>
<div class="cols_body_sport">Boy's Basketball</div>
<div class="cols_body_headlines"><?php
$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "1";
$lim = "3";
$page = "/site/news/news.php";
include($tpath . "headlines.php");
?></div>
<div class="cols_body_sport">Girl's Soccer</div>
<div class="cols_body_headlines">Headlines about Sport</div>
<div class="cols_body_sport">Girl's Softball </div>
<div class="cols_body_headlines">Headlines about Sport</div>
<div class="cols_body_sport">Boy's Tennis </div>
<div class="cols_body_headlines"><?php
$tpath = "/Applications/MAMP/htdocs/site/news/";
$c = "2";
$lim = "3";
$page = "/site/news/news.php";
include($tpath . "headlines.php");
?></div>
<div class="cols_body_sport">Boy's Volleyball </div>
<div class="cols_body_headlines"></div>
<div class="cols_body_sport">Track</div>
<div class="cols_body_headlines">Headlines about Sport</div>
</div>

Let me know if you need anything else. Thanks!

jatar_k

11:32 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the headlines.php code

the stuff that actually retrieves and formats the news

>> We are using a news script that generates the little snippets of code.

that code

Webmattster

2:01 am on Apr 25, 2007 (gmt 0)

10+ Year Member



Here is the code for the headlines.php...sorry about that:

<?
### headlines.php - added [v1.12]

// Automatically get $tpath to avoid possible security holes
$tpath = realpath(__FILE__);
$tpath = substr($tpath,0,strrpos($tpath,DIRECTORY_SEPARATOR)+1);
// Check if the file exists on local server and include it
if(file_exists($tpath . "cn_config.php")) {
require_once($tpath . "cn_config.php");
} else {
die("Could not include required configuration file");
}

// Check if a connection to the database was established
if(!isset($link)) {
die("Please make sure the \"\$tpath\" veriable is the root path to where 'headlines.php' is on your server.");
}

// Page URL to link the news items
if(!isset($page)) { $page = "/index.php"; }
// Set limit for number of items displayed
if(!isset($lim)) { $lim = "5"; }
// Number of characters to cut news titles at
if(!isset($charnum)) { $charnum = "18"; }

// Get news items from database, and order them from newest to oldest
if($c!= "") { $t_news .= " WHERE cat = '$c'"; }
$q['info'] = mysql_query("SELECT * FROM $t_news ORDER BY date DESC LIMIT 0, $lim", $link);

while($h = mysql_fetch_array($q['info'], MYSQL_ASSOC)) {
// Edit the HTML code below for the output of your headlines
// Edit the HTML code between the dashed lines
// ------------------------------------------------------------------
?>
&nbsp;&#187; <a href="<? echo $page;?>?a=<? echo $h['id'];?>" title="<? echo $h['subject'];?>"><? echo cn_cutstr($h['subject'],"$charnum");?></a><br />
<?
// ------------------------------------------------------------------
}
?>


THANKS

jatar_k

12:20 pm on Apr 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so to convert some script to a function you need to

find variables used from outside the snippet and make them parameters in the function declaration

remove any output, add it to a variable and return that variable

I can't see why you need to set $tpath, as it is set at the beginning of that code anyway. I didn't include it in the function declaration.

this might work

these are the variables it needs passed to it
$c
$lim
$page
$link // database connection
$charnum

function get_headlines ($link,$page,$c,$lim,$charnum) {
// Automatically get $tpath to avoid possible security holes
$tpath = realpath(__FILE__);
$tpath = substr($tpath,0,strrpos($tpath,DIRECTORY_SEPARATOR)+1);
// Check if the file exists on local server and include it
if(file_exists($tpath . "cn_config.php")) {
require_once($tpath . "cn_config.php");
} else {
die("Could not include required configuration file");
}

// Check if a connection to the database was established
if(!isset($link)) {
die("Please make sure the \"\$tpath\" variable is the root path to where 'headlines.php' is on your server.");
}

// Page URL to link the news items
if(!isset($page)) { $page = "/index.php"; }
// Set limit for number of items displayed
if(!isset($lim)) { $lim = "5"; }
// Number of characters to cut news titles at
if(!isset($charnum)) { $charnum = "18"; }

// Get news items from database, and order them from newest to oldest
if($c!= "") { $t_news .= " WHERE cat = '$c'"; }
$q['info'] = mysql_query("SELECT * FROM $t_news ORDER BY date DESC LIMIT 0, $lim", $link);
while($h = mysql_fetch_array($q['info'], MYSQL_ASSOC)) {
// Edit the HTML code below for the output of your headlines
// ------------------------------------------------------------------
$headlines = '&nbsp;&#187; <a href="' . $page . '?a=' . $h['id'] . '" title="' . $h['subject'] . '">' . cn_cutstr($h['subject'],"$charnum") . '</a><br />';
// ------------------------------------------------------------------
}
return $headlines;
}

You include the file this function is in at the top of your script and then you might be able to call it like this. I am guessing the $link (connection to database is set somewhere above this)

$c = "1";
$lim = "3";
$page = "/site/news/news.php";
$charnum = '18';// not sure but it is in there

$sporthls = get_headlines($link,$page,$c,$lim,$charnum);
echo $sporthls;

jatar_k

12:26 pm on Apr 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I realize this

cn_cutstr

seems to be a custom function as well so I am hoping that function is already included

reference for functions
[php.net...]

Webmattster

4:25 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



I was not able to get this to work. I'm not sure if I even did it right. Maybe it's just not possible with this type of script?

jatar_k

4:32 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it should be possible

where did you have trouble?

Webmattster

4:36 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



I guess because of my lack of PHP experience I had trouble. I copied everything you said to do and it didn't work. At the end you put something containing $sporthls. Where does this get defined and what do it do? And the get_headlines function, I don't understand what that does maybe I put that one in wrong.

jatar_k

6:01 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this was just an example of what would be on the page where you wanted the headlines to show

you would include the file containing the function near the beginning of the script and then later on you could have something like this

$c = "1";
$lim = "3";
$page = "/site/news/news.php";
$charnum = '18';// not sure but it is in there

$sporthls = get_headlines($link,$page,$c,$lim,$charnum);
echo $sporthls;

the "echo $sporthls;" would spit out your headlines, it's just a varname I chose