...

View Full Version : limit characters



boeing747fp
12-23-2003, 01:36 PM
how do i limit the number of characters displayed to 25 characters then display a '...' using the printf or sprintf function ?

Celtboy
12-23-2003, 02:21 PM
Not one to redo what's been done....


Try here:
http://www.codingforums.com/showthread.php?s=&threadid=29425


here:
http://www.codingforums.com/showthread.php?s=&threadid=29979


and here:
http://www.codingforums.com/showthread.php?s=&threadid=30385

mordred
12-23-2003, 02:32 PM
I'm not sure this would work at all with printf(). AFAIK, you can't specify a maximum length of the string displayed that cuts it's end off. I'm happy if someone else knows how to do it though.

But celtboy gave you enough alternatives, so take your pick.

boeing747fp
12-23-2003, 03:04 PM
how come the following shows up blank? it is supposed to show the first 30 characters of the database....


<?php
$aa = mysql_connect("localhost", "", "");
$db = mysql_select_db("whmx_store",$aa);
$sql_select = "SELECT * FROM cds top1 ORDER BY dateofentry DESC LIMIT 1";
$result = mysql_query($sql_select) or die ("Queryproblem: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
substr("<font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>", 0, 30);
}
?>

Nightfire
12-23-2003, 03:10 PM
I would've thought it would be from this line

$sql_select = "SELECT * FROM cds top1 ORDER BY dateofentry DESC LIMIT 1";

I didn't think you could search two tables at the same time like that within the same query

boeing747fp
12-23-2003, 03:29 PM
no... top1 makes it so that it selects the newest-entered entries in the db.... the table is cds and it works if i just say print(string) but when i go to substr() it doesnt

raf
12-23-2003, 03:32 PM
maybe i'm just stupid, but i don't see any printor echo before the substr()

me'
12-23-2003, 05:57 PM
Originally posted by boeing747fp
no... top1 makes it so that it selects the newest-entered entries in the db... If my limited understanding of MySQL is correct, then:

No it doesn't. ORDER BY dateofentry DESC LIMIT 1 selects the most recent one, not top1.

Nightfire
12-23-2003, 07:21 PM
That's what me and the mysql docs think too :confused:

raf
12-23-2003, 07:22 PM
me' is correct. TOP1 is the JetSQL-version of LIMIT1

(well, it's his buggy little brother since it doesn't make a correct cutoff when there are similar values)
Your query will definitely produce an error and there wount be a recordset.

the
while ($row = mysql_fetch_array($result)) {

is also unnesecary since the recordset will contain maximaly 1 record.
you should also check the numer of returned rows, before processing the recordset.


if (mysql_num_rows($result)== 0){
$echo ('<strong>No rows found.</strong>') ;
} else {
$row = mysql_fetch_array($result) ;
mysql_free_result($result);
...
}

Then about the substring: i can't imagen that the result of

substr("<font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>", 0, 30);

could be of any use. I mean, you don't know what the value of the recordsetvariable will be so you don't know h cutoff, and besides, it's hard to read like that + the result of you function isn't printed, not saved in a variable, so nothing will be visible.
and it will generate invalid html-code...

boeing747fp
12-23-2003, 11:52 PM
how come this doesnt work? it just shows nothing on the page and screws up the page arrangement.
-------------

<?php
$aa = mysql_connect("localhost", "", "");
$db = mysql_select_db("whmx_store",$aa);
$sql_select = "SELECT * FROM cds ORDER BY dateofentry DESC LIMIT 5";
$result = mysql_query($sql_select) or die ("Queryproblem: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$abcd = "<a href='$row[4]'><img src='$row[2]' width='15' height='15' border='0'></a> - <font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>";
print substr($abcd, 0, 30);
print "...<br>";
}
?>

raf
12-24-2003, 12:40 AM
try looking at your pages source in the browser. Micht be that yu have something like

<a href='http://www.retgyhf.co

inthere (the first 30 positions due to your mysterious substr feature that will never generate whatever you're trying to get) which obviously wount generate any visible output (read my previous posts)

Maybe try it first without the substr()

boeing747fp
12-24-2003, 01:07 AM
http://store.whmx.com
----------------------
that's the site... and it is right now without the substr and in the section with "Newest Hits" i only want the results to be one line and stop.

raf
12-24-2003, 01:18 AM
I suppose you want only the first 30 visibel characters then.
So not

Newsboys - Adoration: Worship
Album

but

Newsboys - Adoration: Worship...


So then you need to use the substr on the linktext (the part betwee the <a></a> tags.
so that would be

$link=($row[1] ." - " . $row[0] );
print ("<a href='$row[4]'><img src='$row[2]' width='15' height='15' border='0'></a> - <font face='arial' size='1'><a href='$row[4]'>" . substr($link,0,30) . " ...</a></font>");


This will work, but it's not exactly clean, because it will chop of at position 30, but it would be nicer to chop of at the end of a word.

boeing747fp
12-24-2003, 01:37 AM
thanks... that works

raf
12-24-2003, 01:59 AM
You're welcome.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum