Jump to content

problem with code - strlen then a link


Ameslee

Recommended Posts

<?php
$hostname = localhost;
$username = ;
$password = ;


$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
connection = mysql_select_db("greenac_VORNExpo", $conn);


$query = "SELECT * FROM news";
$mysql_result=mysql_query($query,$conn);
$nothingInDatabase= true;
$i=1;
echo("<table width=100%>");
while(($row = mysql_fetch_array($mysql_result)) && ($i<=2)){
if ($nothingInDatabase) $nothingInDatabase = false;
$row[1] = wordwrap($row[1], 80, '<br />', true);

if (strlen($row[1]) > 100) {
	$row[1] = substr($row[1],0,100);
	$row[1] .= "<a href=\"news.php\">  More</a>";

}	

echo "<tr>";
echo "<td>$row[1]</td></tr>";
echo "<hr>";			

$i++;
}
if ($nothingInDatabase == true) {
echo "<tr><td>Please check back later for entries</td></tr>";
}
?>

 

Ok i have two problems - there isnt any space between the records, and i have 2 records displaying and once the more link is pressed, both of the records are displayed on the page in full, where i wont one displayed at a time. hope this makes sense.

Link to comment
Share on other sites

ok sure,

 

at the moment i have to records showing, which is a paragraph of text.  Only a certain amount is showed on the first page and once you click on the more link i was hoping for it to only display the rest of the content for that particualr record, but at the moment it is displaying both records when the more link is clicked. 

 

Second problem is the spacing between records, i have a <hr> but it seems to be displaying in the wrong spot - above the records instead of in between.

 

Hope i have explained myself better.

 

thanks

Link to comment
Share on other sites

news.php

<?php
			$hostname = localhost;
			$username = ;
			$password = ;


			$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
			$connection = mysql_select_db("greenac_VORNExpo", $conn);


			$query = "SELECT * FROM news";
			$mysql_result=mysql_query($query,$conn);
			$nothingInDatabase= true;

			echo("<table width=100%>");
			while ($row = mysql_fetch_array($mysql_result)) {
			if ($nothingInDatabase) $nothingInDatabase = false;
			$row[1] = wordwrap($row[1], 80, '<br />', true);

			echo "<tr>";
			echo "<td>$row[1]</td></tr>";

			}
			if ($nothingInDatabase == true) {
				echo "<tr><td>Please check back later for entries</td></tr>";
				}
			?>

Link to comment
Share on other sites

It sounds like what your saying is:

 

if the full string is: "this is some random string"

 

and the first page shows: "this is some..."

 

if the user clicks More, you want it to only show "random string" instead of "this is some random string"

 

If this is the case, you will need to remove the first part of that string based on the other page.

 

So if you do substr($content, 100); you will only get the remaining text.

Link to comment
Share on other sites

ok thanks i have that:

 

here is the code now:

 

<?php
$hostname = localhost;
$username = ;
$password = ;


$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("greenac_VORNExpo", $conn);


$query = "SELECT * FROM news";
$mysql_result=mysql_query($query,$conn);
$nothingInDatabase= true;
$i=1;
echo("<table width=100%>");
while(($row = mysql_fetch_array($mysql_result)) && ($i<=2)){
if ($nothingInDatabase) $nothingInDatabase = false;
$row[1] = wordwrap($row[1], 80, '<br />', true);

if (strlen($row[1]) > 100) {
	$row[1] = substr($row[1],0,100);
	$row[1] .= "<a href=\"news.php\">  More</a>";

	}	

echo "<tr>";
echo "<td>$row[1]<hr></td></tr>";

$i++;

}
if ($nothingInDatabase == true) {
echo "<tr><td>Please check back later for entries</td></tr>";
	}
	?>

 

and the code for news.php

 

<?php
$hostname = localhost;
$username = ;
$password = ;


$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("greenac_VORNExpo", $conn);

$query = "SELECT * FROM news";
$mysql_result=mysql_query($query,$conn);
$nothingInDatabase= true;

echo("<table width=100%>");
while ($row = mysql_fetch_array($mysql_result)) {
if ($nothingInDatabase) $nothingInDatabase = false;
$row[1] = wordwrap($row[1], 80, '<br />', true);

echo "<tr>";
echo "<td>$row[1]</td></tr>";

}
if ($nothingInDatabase == true) {
echo "<tr><td>Please check back later for entries</td></tr>";
				}
			?>

 

How do i display only one record at a time on the news.php page after clicking on the more link on the event.php.

 

hope u can understand this?

Link to comment
Share on other sites

Hmm... I understand now. You need to be linking with an ID. Your news.php link should instead link like this:

<a href="link.php?id=<?=$row['Id'];?>>Read More</a>

 

-Or use whatever column name your using as the Primary Key.

 

Then on the next page, do something similar to this:

 

$id=$_GET['id'];

$sql="SELECT * FROM news WHERE `Id` = '{$id}'";

Link to comment
Share on other sites

i keep getting this error now:

 

 

Parse error: syntax error, unexpected '<' in /home/greenac/public_html/events.php on line 260

line 260 is the last line so its probably this line that was just put in:

$row[1] .= <a href="news.php?id=<?=$row[1];?>>Read More[/url]

Link to comment
Share on other sites

Ok, the [/url] thing is apparently the result of this forum replacing my end a href tag. Your string should be:

 

$row[1] .= "<a href=\"news.php?id={$row[1]}\">Read More</a>";

 

In order to end this thread; if you still can't get it working, pm me and I will talk to you on instant messenger.

Link to comment
Share on other sites

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.