vinpkl Posted October 25, 2008 Share Posted October 25, 2008 hi all i have index page and description page. In description page i am able to show the complete list of the features of my products which are fetched from database in 20 lines. Means each product has a long description of 20 lines each. Now i want to show only 4 lines out of those 20 lines in my index page for my each product. how can i fetch only 4 lines out of those 20 lines. the name of my description field in my form is "description". vineet Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/ Share on other sites More sharing options...
wildteen88 Posted October 25, 2008 Share Posted October 25, 2008 In what format is the data in your description field, eg Format 1 Line 1<br />Line 2<br />Line 3 Or, Format 2 Line 1<br /> Line 2<br /> Line 3 Or Line 1 Line 2 Line 3 We'll need to know this in order for us to provide a suitable answer. Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674250 Share on other sites More sharing options...
vinpkl Posted October 25, 2008 Author Share Posted October 25, 2008 In what format is the data in your description field, eg Format 1 Line 1<br />Line 2<br />Line 3 Or, Format 2 Line 1<br /> Line 2<br /> Line 3 Or Line 1 Line 2 Line 3 We'll need to know this in order for us to provide a suitable answer. hi my data is in Format 2. It contains the <ul></li> tags and the data with <li> tags. like bulleted content. <li>line 1</li> <li>line 2</li> Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674255 Share on other sites More sharing options...
wildteen88 Posted October 25, 2008 Share Posted October 25, 2008 Something like the following should do $sql = 'SELECT * FROM your_table'; $result = mysql_query($sql) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $desc = short_description($row['description']); echo $desc; } function short_description($text, $max_lines = 4) { // standardize new lines $text = str_replace(array("\r\n", "\r"), "\n", $text); // get each line $lines = explode("\n", $text); // retieive the requires lines $list = array(); for($i = 0; $i <= $max_lines; $i++) { $list[] = $lines[$i]; } // add closing ul tag $list[] = '</ul>'; return implode("\n", $list); } Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674259 Share on other sites More sharing options...
vinpkl Posted October 25, 2008 Author Share Posted October 25, 2008 Something like the following should do $sql = 'SELECT * FROM your_table'; $result = mysql_query($sql) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $desc = short_description($row['description']); echo $desc; } function short_description($text, $max_lines = 4) { // standardize new lines $text = str_replace(array("\r\n", "\r"), "\n", $text); // get each line $lines = explode("\n", $text); // retieive the requires lines $list = array(); for($i = 0; $i <= $max_lines; $i++) { $list[] = $lines[$i]; } // add closing ul tag $list[] = '</ul>'; return implode("\n", $list); } hi i have used this in my code but its showing the full 20 lines. if(isset($_REQUEST['submit'])) { $model=$_REQUEST['model']; $qry="select * from product_table where product_name LIKE '%$model%' "; $result = mysql_query($qry); if(mysql_num_rows($result)>0) { while($row=mysql_fetch_array($result)) { $desc = short_description($row['detail_description']); echo "<table>"; echo "<tr>"; echo "<td width=148 class=products>Image</td><td width=148 class=products>Product Name</td><td class=products width=148>Description</td><td width=148 class=products>Cutout Price</td><td width=148 class=products>Price</td>"; echo "</tr>"; echo "<tr>"; echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row['image'] . "'/>" . "</td>"; echo "<td valign=top width=125>". $row['product_name'] . "</td>"; echo "<td valign=top>". $row['detail_description'] . "</td>"; echo "<td valign=top class=cut>". "NZ$ " . $row['cutout_price'] . "</td>"; echo "<td valign=top>". "NZ$ " . $row['price'] . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td valign=top colspan=5 align=right>". "<a href=description.php?id=" . $row['product_id'] . ">" ."<img src='images/details.gif' border=0 />" . "</a>" . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td class=products_sep colspan=5>" . "<image src='images/spacer.gif' height=15 width=15>" . "</td>"; echo "</tr>"; echo "</table>"; } } else echo "No Results found !"; } function short_description($text, $max_lines = 4) { // standardize new lines $text = str_replace(array("\r\n", "\r"), "\n", $text); // get each line $lines = explode("\n", $text); // retieive the requires lines $list = array(); for($i = 0; $i <= $max_lines; $i++) { $list[] = $lines[$i]; } // add closing ul tag $list[] = '</ul>'; return implode("\n", $list); } Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674265 Share on other sites More sharing options...
wildteen88 Posted October 25, 2008 Share Posted October 25, 2008 This: echo "<td valign=top>". $row['detail_description'] . "</td>"; Needs to be echo "<td valign=top>". $desc . "</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674268 Share on other sites More sharing options...
vinpkl Posted October 25, 2008 Author Share Posted October 25, 2008 This: echo "<td valign=top>". $row['detail_description'] . "</td>"; Needs to be echo "<td valign=top>". $desc . "</td>"; Hi thanks a lot. Its now working perfect. one thing more i would like to ask that i have a <textarea> and in that i can insert 3 lines. i just want to how can those 3 lines should come with a break after each line. means i just want to add <br> after each line. the name of <textarea> is shipping_detail. <?php echo $row['shipping_detail']; ?> vineet Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674273 Share on other sites More sharing options...
wildteen88 Posted October 25, 2008 Share Posted October 25, 2008 nl2br() will convert newlines to <br /> tags. NOTE: Only nl2br when you're grabbing data from the database. I do not recommend using nl2br when inserting data into the database. Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674431 Share on other sites More sharing options...
vinpkl Posted October 26, 2008 Author Share Posted October 26, 2008 nl2br() will convert newlines to <br /> tags. NOTE: Only nl2br when you're grabbing data from the database. I do not recommend using nl2br when inserting data into the database. hi thanks. i got it working fine vineet Quote Link to comment https://forums.phpfreaks.com/topic/130043-solved-how-to-fetch-4-lines-from-20-lines-of-description/#findComment-674819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.