jacko_162 Posted February 6, 2010 Share Posted February 6, 2010 i am calling an array for multiple fields and displaying them in a pagination on my custom cms script. when i want to echo data i use: <?=$data["content"]?> how can i limit this to say only echo the first 40 characters of the data stored in the database, for example i want it to display like this; "this is some test content and the rest cannot be seen unless.............." obviously the text is wrapped in a <a> tag and is clickable to see the rest of the content. any help would be great Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/ Share on other sites More sharing options...
harristweed Posted February 6, 2010 Share Posted February 6, 2010 To choose the first 40 characters might chop a word in half, I think better to display whole word. // first find the first space after 40 characters, this should be the end of a word $offset =strpos ( $data["content"], " " ,40) ; //then put into variable $shorter_list = substr ($data["content"],0,$offset); //add the trailing dots $shorter_list.="...."; Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007941 Share on other sites More sharing options...
jacko_162 Posted February 6, 2010 Author Share Posted February 6, 2010 thanks, ad how would i echo this? would i replace <?=$data["content"]?> with anything? Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007944 Share on other sites More sharing options...
harristweed Posted February 6, 2010 Share Posted February 6, 2010 If you don't know how to echo variables I suggest you read some basic PHP Tutorials Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007947 Share on other sites More sharing options...
jacko_162 Posted February 6, 2010 Author Share Posted February 6, 2010 so im assuming i have to echo $shorter_list i will have a play around now and let you know. thx for the info harristweed Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007954 Share on other sites More sharing options...
jacko_162 Posted February 6, 2010 Author Share Posted February 6, 2010 ok when i echo the $shorter_list variable i just get the ... echoed and not the "content" field? Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007970 Share on other sites More sharing options...
harristweed Posted February 6, 2010 Share Posted February 6, 2010 this works.... $data["content"]="a load of stuff a load of stuff a load of stuff a load of stuff a load of stuff a load of stuff "; // first find the first space after 40 characters, this should be the end of a word $offset =strpos ( $data["content"], " " ,40) ; //then put into variable $shorter_list = substr ($data["content"],0,$offset); //add the trailing dots $shorter_list.="...."; echo $shorter_list; first echo $data["content"] and make sure it has content Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007971 Share on other sites More sharing options...
jacko_162 Posted February 6, 2010 Author Share Posted February 6, 2010 when i echo $data["content"] i get my data but the moment i echo $shorter_list i get nothing but the dots "...." here is full code for the page, apologies for the tables etc... its already a working page <?php session_start(); ?> <? include("header.php") ?> <? $connect = @mysql_connect("$host", "$username", "$password"); if (!($connect)) // If no connect, error and exit(). { echo("<p>Unable to connect to the database server.</p>"); exit(); } if (!(@mysql_select_db($database))) // If can't connect to database, error and exit(). { echo("<p>Unable to locate the $database.</p>"); exit(); } if (!($limit)){ $limit = 5;} // Default results per-page. if (!($page)){ $page = 0;} // Default page value. $numresults = mysql_query("SELECT * FROM $table3 "); // the query. $numrows = mysql_num_rows($numresults); // Number of rows returned from above query. if ($numrows == 0){ echo("No Results Found, Please try another catagory!"); // bah, modify the "Not Found" error for your needs. exit();} $pages = intval($numrows/$limit); // Number of results pages. // $pages now contains int of pages, unless there is a remainder from division. if ($numrows%$limit) { $pages++;} // has remainder so add one page $current = ($page/$limit) + 1; // Current page number. if (($pages < 1) || ($pages == 0)) { $total = 1;} // If $pages is less than one or equal to 0, total pages is 1. else { $total = $pages;} // Else total pages is $pages value. $first = $page + 1; // The first result. if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) { $last = $page + $limit;} //If not last results page, last result equals $page plus $limit. else{ $last = $numrows;} // If last results page, last result equals total number of results. //escape from PHP mode. ?> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-style-type" content="text/css" /> <meta http-equiv="content-script-type" content="text/javascript" /> <title>Cpanel - News</title> </head> <body> <div id="content" class="basic"> <div id="container"> <div class="inner-container"> <!-- Main content – all content is within boxes. Feel free to add your boxes (see the examples on the bottom of this document) and your content within --> <div class="box box-75 altbox"><!-- .altbox for alternative box's color --> <div class="boxin"> <div class="header"> <h3>News » All</h3> <a class="button" href="addnews.php">Add News »</a><!-- Action button in the header of the box --><br> <table width="100%" border="0" cellspacing="0" cellpadding="2"> <tr> <td><h2>Results <strong><?=$first?></strong> - <strong><?=$last?></strong> of <strong><?=$numrows?></strong></h2></td> <td><div align="right"> <h2> <a class="ico" href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5"><strong><span class="whitelink"> 5</span></strong> <img src="images/results_5.gif" border="0"></a> <a class="ico" href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10"> <strong><span class="whitelink">10</span></strong> <img src="images/results_10.gif" border="0"></a> <a class="ico" href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20"><strong><span class="whitelink"> 20</span></strong> <img src="images/results_20.gif" border="0"></a><img src="images/spacer.gif" width="15" height="1"></h2> </div></td> </tr> </table> </div><div> </div> <div class="content"> <table border="0" cellpadding="0" cellspacing="0"> <thead> <tr> <td><div align="left">Title</div></td> <th><div align="left">Content</div></th> <td width="70" class="tc"><div align="left">Date</div></td> <td width="50" class="tc"><div align="left">Time</div></td> <td width="50" class="tc"><div align="right"></div></td> <? //Go back into PHP mode. // Now we can display results. $results = mysql_query("SELECT * FROM $table3 ORDER BY id ASC LIMIT $page, $limit"); while ($data = mysql_fetch_array($results)) { ?> </tr> </thead> <tbody> <tr class="first"> <td class="tc"><div align="left"> <?=$data["title"]?> </div></td> <td><div align="left"> <?php // first find the first space after 40 characters, this should be the end of a word $offset =strpos ( $data["content"], " " ,40) ; //then put into variable $shorter_list = substr ($data["content"],0,$offset); //add the trailing dots $shorter_list_content="...."; echo $shorter_list; ?> </div></td> <td width="70"><div align="left"> <?=$data["date"]?> </div></td> <td width="50"><div align="left"> <?=$data["time"]?> </div></td> <!-- a.ico-comms for comment-like backgrounds --> <td width="50"><div align="right"><a class="ico" id='tooltip' title='Edit This Item' href="editnews.php?ID=<?=$data["id"]?>"><img src="Includes/css/img/led-ico/pencil.png" border="0" /></a> <a class="ico" id='tooltip' title='Delete This Item' href="remove.php?ID=<?=$data["id"]?>&db=news"><img src="Includes/css/img/led-ico/delete.png" border="0" /></a> <? } ?> </div> </td> </tr> </tbody> </table> <div class="pagination"> <ul> <? if ($page != 0) { // Don't show back link if current page is first page. $back_page = $page - $limit; echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");} for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. { $ppage = $limit*($i - 1); if ($ppage == $page){ echo("<b>$i</b> \n");} // If current page don't give link, just text. else{ echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");} } if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. $next_page = $page + $limit; echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");} ?> </ul> </div></div></div> </div> <div class="box box-25"><!-- box 25% width --> <div class="boxin"> <div class="header"> <h3>Menu</h3> </div> <div class="content"> <ul class="simple"><!-- ul.simple for simple listings of pages, categories, etc. --> <li><strong><a class="ico" title='Index Page' href="index.php"><img src="Includes/css/img/h-ico/Globe.png" border="0" /> Index</a></strong></li><!-- Add or Edit Index Page Main Menu Items --> <li><strong><a class="ico" title='Products Page' href="mainproducts.php"><img src="Includes/css/img/h-ico/cash.png" border="0" /> Products</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Catagories Page' href="catagory.php"><img src="Includes/css/img/h-ico/library bookmarked.png" border="0" /> Catagories</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Manufacturers Page' href="manufacturers.php"><img src="Includes/css/img/h-ico/applications.png" border="0" /> Manufacturers</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='News Page' href="news.php"><img src="Includes/css/img/h-ico/notebook.png" border="0" /> News</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Customer Enquiries' href="enquiries.php"><img src="Includes/css/img/h-ico/unread mail.png" border="0" /> Enquiries</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Edit Site Pages' href="pages.php"><img src="Includes/css/img/h-ico/new doc.png" border="0" /> Pages</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Product User Comments' href="comments.php"><img src="Includes/css/img/h-ico/comments.png" border="0" /> Comments</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='Site Setup' href="setup.php"><img src="Includes/css/img/h-ico/advanced.png" border="0" /> Setup</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> <li><strong><a class="ico" title='About RCPanel' href="about.php"><img src="Includes/css/img/h-ico/info.png" border="0" /> About</a></strong></li><!-- DONT FORGET TO EDIT TOOLTIP! --> </ul> </div> </div> </div> </div> <!-- .inner-container --></div></div> </body> appreciate any help Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1007975 Share on other sites More sharing options...
jacko_162 Posted February 7, 2010 Author Share Posted February 7, 2010 can anyone help me? google isnt answering my questions Link to comment https://forums.phpfreaks.com/topic/191162-limit-data-from-array/#findComment-1008137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.