Forum Moderators: coopster
<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>
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?
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
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?
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?
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.
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.
<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!
<?
### 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
// ------------------------------------------------------------------
?>
» <a href="<? echo $page;?>?a=<? echo $h['id'];?>" title="<? echo $h['subject'];?>"><? echo cn_cutstr($h['subject'],"$charnum");?></a><br />
<?
// ------------------------------------------------------------------
}
?>
THANKS
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 = ' » <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;
cn_cutstr
seems to be a custom function as well so I am hoping that function is already included
reference for functions
[php.net...]
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