I am trying to truncate variables from a mysql database, so when I show my display of the database in a table, it limits the characters shown.

so in my code I have an array that spits out my variables into a table,

I grab the data through, my query.

$sql="SELECT * FROM _products WHERE pending='0' AND deleted='0' ORDER BY productID ASC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

then I have my data output in the table, so I can click edit on it, to change things.

<td><? echo $rows['productID']; ?></td>  
<td><? echo $rows['pageName']; ?></td> <td><? echo $rows['section1']; ?></td> <td> <? echo $rows['section2']; ?></td>
<td><? echo $rows['customer']; ?></td>

I know shorthand, I'm changing my codes so there not that way..

I was going to try to use substr_replace, but thats for strings not variables??, but can I put my variable into a string, then limit it, and display the string instead of the variable in this example?

I tried a few things but got very lost.

I found the example on the main page talking about substr_replace to truncate, so that is what I'm trying to do with my section1, section2 pieces of my database table when displayed.

Example from this page

$article = "BREAKING NEWS: In ultimate irony, man bites dog."; 
$summary = substr_replace($article, "...", 40); // $summary = "BREAKING NEWS: In ultimate irony, man bi..."
    mnewbegin wrote:

    I was going to try to use substr_replace, but thats for strings not variables?

    The value of your variable is a string, so there is no problem.

      I guess I just don't know the correct usage of it then.

      where do I fill in the substr_replace code? I tried placing it in front of the variable..

      but there seems to be issues.

      do i declare it right above it?

        As laserlight points out, you're already dealing with strings. A "variable" isn't a data type - it's simply a container that can hold data of any data type.

        Plus, PHP is a very loosely-typed language, so things like this:

        $int = 1234567;
        var_dump( substr($int, 1, 2) );

        work just fine, giving an output of:

        string(2) "23"

        EDIT:

        mnewbegin wrote:

        where do I fill in the substr_replace code? I tried placing it in front of the variable..

        You'd use it just like the example code shows. Show us the exact code that you tried.

          I guess where my problem lies is.

          My variable is all ready put into an array?

          So I don't know placement in the array.. that it gets shortened or truncated.

          I have, that array spit out into a table, so it shows me the individual items in the table.

          The section1 and section2, parts are like 300 characters long and I only want to show about 40 then. ..... after it.

          <td><? echo $rows['productID']; ?></td>  
          <td><? echo $rows['pageName']; ?></td> <td> <? echo $rows['section1']; ?></td>
          <td> <? echo $rows['section2']; ?></td>
          <td><? echo $rows['customer']; ?></td>
          <td><? echo $rows['amazonSearchIndex']; ?></td> <td><? echo $rows['brand']; ?></td> <td><? echo $rows['amazonSearch']; ?></td> <td><? echo $rows['store_id']; ?></td> <td><? echo $rows['added_on']; ?></td> <td><? echo $rows['price']; ?></td>

          but I don't know usage of it, when I'm not declaring the string right above it? the data is being pulled and put into the array, can I take the array variable and truncate that? with something like this?

          I tried this but Im sure the placement is wrong? if this even works?

          <td><? echo $rows['productID']; ?></td>  
          <td><? echo $rows['pageName']; ?></td> <td><? var_dump( substr($rows[$section1], 0, 40) )?>;</td> <td> <? echo $rows['section2']; ?></td>
          <td><? echo $rows['customer']; ?></td>
          <td><? echo $rows['amazonSearchIndex']; ?></td> <td><? echo $rows['brand']; ?></td> <td><? echo $rows['amazonSearch']; ?></td> <td><? echo $rows['store_id']; ?></td> <td><? echo $rows['added_on']; ?></td> <td><? echo $rows['price']; ?></td>

          but I get a...

          bool(false) ;

          error message?

            Sorry and with this..

            <td><? var_dump( substr($rows[section1], 0, 40) )?>;</td> 
            


            string(40) "This is a test this is a test this is a " ;

            but it outputs the word string(40) in front of the string that I want?

              mnewbegin wrote:

              but it outputs the word string(40) in front of the string that I want?

              Yeah, that is just meta-information concerning the value, i.e., it is of string type with a length of 40.

                ok, so now that I'm a little more educated... LOL..

                if i want to use this with substr_replace

                and turn it into

                "This is a test this is a test this is a " ;

                I take my current code of.

                <td><? var_dump( substr_replace($rows[section1], "...", 40) )?>;</td> 
                
                or
                
                <td><? var_dump( substr($rows[section1], "...", 40) )?>;</td>
                
                
                

                how do I remove the meta data from showing?

                the example here shows no meta data on it?
                and do what with it?

                Right now my string spits out.

                string(43) "This is a test this is a test this is a ..." ;

                but I just want,

                This is a test this is a test this is a ...

                  ...which is why you should stick to using [man]echo[/man] rather than vardump.

                  Also, in the code below:

                  $array = array('myIndex' => 'Hello world!', 'foo' => 'bar');
                  
                  $message = "Hello world!";

                  $message and $array['myIndex'] (where $array is meant to simulate your $rows variable above) are identical. Same data type, same contents, etc. There's no difference between the two of them as far as the data is concerned.

                  EDIT: Also note that this:

                  $rows[section1]

                  should be this:

                  $rows['section1']

                  since the array index "section1" is a string, and all strings must be delimited by quotes. Otherwise, PHP thinks you're referring to a constant (man page: [man]language.constants[/man]) named section1.

                    thank you both, so much.

                    Its working now, and I have a better understanding of how it works now. I can now build it into my site correctly.

                      Write a Reply...