CodingSimple PHP and SQL

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  ANT_THOMAS  
 To:  ALL
32051.1 

I've got a PHP script to pull some stuff from an SQL database, basically a simple blog.

 

I know how to get it to just pull 10 or however many rows from the table, but what I want to be able to do is just pull say 50 words from the main part of the row.

 

So on a front page there is a snippet of the article at hand.


Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.2 In reply to 32051.1 
Get a rough snippet with MySQL:
code:
left(text,300)


Then use a RegEx in PHP to ensure you're not splitting off any words:
code:
/(\w+\W*?){1,50}/


Something like that should work.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.3 In reply to 32051.2 
I'm rubbish at this.

This is my code:
PHP code:
<?
include("dbconnect.php");
if (empty($offset)) {
$offset='0';
}
$start = $offset;
if (isset($_GET['start'])) $start = $_GET['start']; 
$next = $start + 10;
$previous = $start - 10;
if ($previous < 0) $previous = 0;
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM news WHERE id=$id";
    $result = mysql_query($sql);
}
$getnews = mysql_query("select * from news ORDER BY id DESC LIMIT " . $start . ",2");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("
 
 
</div>
<div class='blog-title'>  
<table width='100%' border='0' cellpadding='0'>
    <tr>
      <td>$title</td>
      <td align='right'>$date @ $ip_address</td>
    </tr>
  </table></div>
<div class='blog-body'>$news </br></br></div> 
 
</div>
<br>
<br>
 
");
}
?>


I assume it goes somewhere in the $getnews query?

Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.4 In reply to 32051.3 
quote:
I assume it goes somewhere in the $getnews query?


Yep. As a general rule you should never do "SELECT *", but always specify the precise columns you want.

Specifically, the query should initially read like this:
SQL code:
SELECT id
     , title
     , date
     , ip_address
     , news
FROM news
[etc]


And you want to update it to be:
SQL code:
SELECT id
     , title
     , date
     , ip_address
     , LEFT(news,300) AS news_brief
FROM news
[etc]


That will get the first three hundred characters, but may well cut off in the middle of a word, which you don't want.

So, in your big echo, instead of just $news, you want this:
PHP code:
if (strlen($news_brief) < 300)
{
// less than 300 chars, so not cut off in middle of word; show full thing.
echo $news_brief;
}
else
{
// 300 or more characters, so remove any part-words from end of string by having it end in whitespace.
preg_match('/(\S+\s*?){1,50}/', $news_brief, $nb_fullwords);
echo $nb_fullwords[0] . "...";
}

Obviously you'll need to end the initial echo for that and start a new one.

You might notice I've modified the main regex bit from (\w+\W*?) to (\S+\s*?) - that's because I just realised the first one could cut URLs up, whilst the second one shouldn't.
(\w = any word character, \W means non-word character, whereas \s and \S are whitespace and non-whitespace respectively. \W would (unwantedly) match punctuaction, whilst \s wont.)


Oh, and I can't resist pointing out that you'd be much better off with simpler HTML than you have there, but I wont go on about it unless you want me to. ;)
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  ANT_THOMAS     
32051.5 In reply to 32051.3 
just so you know ....

you really ought to be escaping those things before they go into sql, or someone could edit the querystring to have

code:
?start=blah'; DROP DATABASE; #


(or something similar ... I can't remember the syntax exactly off the top of my head).

If they did that, it'd drop your database, which is bad.

You may be fairly safe already if your server has magic quotes turned on, and if you are aware of all the above but let it go because of magic quotes .... sorry! Just trying to help.

I'd also change the

code:
if (isset($_GET['start'])) $start = $_GET['start']; 


to

code:
if (is_numeric($_GET['start'])) $start = $_GET['start']; 


Anything other than numbers will cause breakage, and its more secure if you only allow numbers.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.6 In reply to 32051.4 

Thank You very very much!

 

The first bit worked a treat cutting it down to 300 characters, but the keeping full words regex didn't. Not sure why. Not really an issue tho.

 

My next issue is, in my first code there was a bit to only display a certain amount of entries, the most recent 2. I need this again, what and where?!?


Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.7 In reply to 32051.6 
That's the ORDER BY id DESC LIMIT " . $start . ",2" bit. Put that where the [etc] bit is.

I might have figured why the Regex bit isn't working - change the * to a + after the lowercase \s and see if that helps.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.8 In reply to 32051.7 
That's great, the first bit works.

The error for the regex is now on this line...

PHP code:
echo $nb_fullwords[0] . "...";


and the error it's giving is..

code:
Parse error: syntax error, unexpected '.' in /home/wakeupb/public_html/blog/indextest1.php on line 59

Antoine
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  ANT_THOMAS     
32051.9 In reply to 32051.8 

i think the error is on the line above - missing ; at the end maybe?

 

i pasted your line into a script i am working on here, and no parse error (which i did because it looked right).

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.10 In reply to 32051.8 
Hmmm, it's giving the error on the concatenation thingy, which I'm fairly sure is correct, but maybe try just using
PHP code:
echo $nb_fullwords[0];
to see if that works?
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.11 In reply to 32051.10 
I think the issue is coming from the big echo somewhere. I assume there is an obvious thing wrong there that I can't see...

PHP code:
echo("
 
 
</div>
<div class='blog-title'>  
<table width='100%' border='0' cellpadding='0'>
    <tr>
      <td>$title</td>
      <td align='right'>$date @ $ip_address</td>
    </tr>
  </table></div>
<div class='blog-body'>if (strlen($news_brief) < 300)
{
// less than 300 chars, so not cut off in middle of word; show full thing.
echo $news_brief;
}
else
{
// 300 or more characters, so remove any part-words from end of string by having it end in whitespace.
preg_match('/(\S+\s+?){1,50}/', $news_brief, $nb_fullwords);
echo $nb_fullwords[0];
}<a href='LINK'>[read more]</a></br></br></div> 
 
</div>
<br>
<br>
 
");

Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.12 In reply to 32051.11 
Ah. You know the bit where I said "you'll need to end the initial echo for that and start a new one" — you needed to actually do that bit. :P

Like this:

PHP code:
echo("
 
 
</div>
<div class='blog-title'>  
<table width='100%' border='0' cellpadding='0'>
    <tr>
      <td>$title</td>
      <td align='right'>$date @ $ip_address</td>
    </tr>
  </table></div>
<div class='blog-body'>
");
 
if (strlen($news_brief) < 300)
{
// less than 300 chars, so not cut off in middle of word; show full thing.
echo $news_brief;
}
else
{
// 300 or more characters, so remove any part-words from end of string by having it end in whitespace.
preg_match('/(\S+\s+?){1,50}/', $news_brief, $nb_fullwords);
echo $nb_fullwords[0];
}
 
echo ("
<a href='LINK'>[read more]</a></br></br></div> 
 
</div>
<br>
<br>
 
");
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.13 In reply to 32051.12 
Haha, what an idiot, right, one step further now, but still an error.

code:
Parse error: syntax error, unexpected $end in /home/wakeupb/public_html/blog/indextest1.php on line 76


Line 76 being the last line of the code

HTML code:
</html>


So the last bit is...

PHP code:
echo("
 
 
</div>
<div class='blog-title'>  
<table width='100%' border='0' cellpadding='0'>
    <tr>
      <td>$title</td>
      <td align='right'>$date @ $ip_address</td>
    </tr>
  </table></div>
<div class='blog-body'>
");
 
if (strlen($news_brief) < 300)
{
// less than 300 chars, so not cut off in middle of word; show full thing.
echo $news_brief;
}
else
{
// 300 or more characters, so remove any part-words from end of string by having it end in whitespace.
preg_match('/(\S+\s+?){1,50}/', $news_brief, $nb_fullwords);
echo $nb_fullwords[0];
}
 
echo ("
<a href='LINK'>[read more]</a></br></br></div> 
 
</div>
<br>
<br>
 
");
 
?>
 
</html>

Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.14 In reply to 32051.13 
Hmmm, can't see anything obviously wrong there.

What are you editing with?
An editor with syntax highlighting should be able to highlight any unclosed strings/functions/etc.

If you can be arsed download & setting it up, I use PHPEclipse which has syntax highlighting (and various other useful features), and should highlight if there's anything unclosed in the file.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.15 In reply to 32051.14 
I've been using <cough>Dreamweaver</cough>, I'll give PHPEclipse a go.

Antoine
0/0
 Reply   Quote More 

 From:  ANT_THOMAS  
 To:  Peter (BOUGHTONP)     
32051.16 In reply to 32051.14 

Found it! Sorted now!

 

Thanks loads for the help.


Antoine
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS     
32051.17 In reply to 32051.16 
Not a problem. :)
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  Peter (BOUGHTONP)     
32051.18 In reply to 32051.14 
i've not got on with that. I use Zend Dev Environment, but its a bit annoying at times, it likes to pause for a second randomly.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Ben (BENLUMLEY)     
32051.19 In reply to 32051.18 
Well I don't really get on with any editors, but I use Eclipse often enough for other stuff to cope with its various annoyances.
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  Peter (BOUGHTONP)     
32051.20 In reply to 32051.19 
i'd imagine you to be a vim user.
0/0
 Reply   Quote More 

Reply to All    
 

1–20

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats