Jump to content

[SOLVED] how to fetch 4 lines from 20 lines of description


vinpkl

Recommended Posts

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

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.

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>

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);
}

 

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);
}	

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.