Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    removing empty fields

    looked around for a way to do this:
    I have an application that splits up blocks of text into sentences based on punctuation, and sometimes, like if a user types in "I'm so stoked!!" those two "!"s get turned into empty parts of the array. I'd just like to run a little command at the end of my storage page that deletes all the empty fields in the db, that way maintenence is happening while it's being used rather than having to run a seperate script to maintain the db.
    So far I have:
    PHP Code:
    $query "SELECT * FROM sentences WHERE sentence IS NULL"
    heheh...what next?
    I think the other option is to step through my array and delete any empty parts....the whole prog. is attached below, if you feel like looking through it. but i'd like to know how to do this (above).
    thanks!
    PHP Code:
    <?php
    ob_start
    ();
    //connect to server
    include('connection.php');
    //show the $_POST['body'] array as it came to the page
    echo "<br>as it was entered:<br>";
    echo 
    $_POST['body']; 
    //show the $_POST['createdate'] as it came to the page
    echo "<br>datetime:<br>";
    echo 
    $_POST['createdate'];
    //make the date
    $date $_POST['createdate'];
    echo 
    "<br>";
    echo 
    $date;
    //make the body and turn special chars into html
    $bodyhtmlentities($_POST['body'], ENT_QUOTES);
    echo 
    "<br> body array:";
    echo 
    "$body";
    //put the body into the letters table
    mysql_select_db($database$connection);
    $query "INSERT INTO text (body, createdate) VALUES ('$body', '$date')";
    $result mysql_query($query); 
    //error reporting below:
    if (!$result)
    {
     echo 
    "<b>A fatal MySQL error occured</b>.\n<br />Query: " $query "<br />\nError: (" mysql_errno() . ") " mysql_error();
    }
    //stripslashes
    $sentences stripslashes($body);
    echo 
    "<br>stripped:";
    echo 
    "$sentences2";
    //split it into bits
    $sentences2 split('[,.!?]'$sentences100);
    print 
    "<br>the final array:<br>";
    print_r(array_values($sentences2));
    //pop off the end of it to avoid line breaks causing problems
    $sentencesshort array_pop($sentences2);
    print 
    "<br> the popped array:<br>";
    print_r(array_values($sentences2));
    //foreach split up array
    foreach($sentences2 as $singlesentence){
    //put the single sentences into the table
    mysql_select_db($database$connection);
    $query "INSERT INTO sentences (sentence, createdate) VALUES ('$singlesentence', '$date')";
    $result mysql_query($query); 
    //error reporting
         
    if (!$result)
    {
         echo 
    "<b>A fatal MySQL error occured</b>.\n<br />Query: " $query "<br />\nError: (" mysql_errno() . ") " mysql_error();
         }
    }
    //database maintenence (getting rid of those pesky nulls...)
    mysql_select_db($database$connection);
    $query "SELECT * FROM sentences WHERE sentence IS NULL";
    $result mysql_query($query); 
    //error reporting
         
    if (!$result)
    {
         echo 
    "<b>A fatal MySQL error occured</b>.\n<br />Query: " $query "<br />\nError: (" mysql_errno() . ") " mysql_error();
         }
    ob_end_flush();
    ?>

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe i don't understand, but i think yur just making it hard on yourself.

    You need to prevent 'empty' rows from getting entered. Like
    PHP Code:
    foreach($sentences2 as $singlesentence){ 
    //put the single sentences into the table 
        
    if (strlen($singlesentence) >= 1){
              
    mysql_select_db($database$connection); 
              
    $query "INSERT INTO sentences (sentence, createdate) VALUES ('$singlesentence', '$date')"
    $result mysql_query($query);  
    //error reporting 
              
    if (!$result
              { 
                   echo 
    "<b>A fatal MySQL error occured</b>.\n<br />Query: " .     $query "<br />\nError: (" mysql_errno() . ") " mysql_error(); 
              }
        } 

    But the way you split up the text doesn't look correct. I mean, you will loose the ",.!?" no? Or isn't that a problem?

    Maybe if you tell us what your actua goal is, we can point you to a more elegant sollution...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey there raf,
    thanks for your diligence in helping solve these problems. losing the punctuation isn't a problem because when I display it I want to auto-insert periods (although, for some reason that isn't working...more later on that) to sort of "sterilize" the text (this is for an avant garde art project at college..)
    I LOVE strlen! that's grrreat! I'll add it right now.
    thanks
    syd


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •