Current location: Hot Scripts Forums » Programming Languages » PHP » Need hints on merging text variables into only one


Need hints on merging text variables into only one

Reply
  #1 (permalink)  
Old 04-11-04, 10:14 PM
Frank Frank is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Montreal, Canada
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Need hints on merging text variables into only one

Hi everyone, I am currently working to do my webpage using a templating system which will replace tags by text (ie: {HOME} to "Home") and it's doing good

The only problem I have now is when I want to replace one tag with lots of text such as tables with data. I tried this in a loop so it would do the same thing for every row of data that is in the mySQL table.

PHP Code:

$temp '<p align="left"><b>[<u>'$myrow['id'] .'</u>]</b> 'nl2br($myrow['text']) .'</p>'.

$blabla "$blabla$temp"
As you can see, it should replace $temp with the informations from the row in the mySQL table then add it to the $blabla variable right after the previous informations from the previous row(s). Like I said, it should work like this but it doesn't work like I thought it would work!

The code will show rows id: #1, #2, #1, #3, #1, #2 and #1 in this order when it should show all 5 rows and never repeat the same one. My table has 5 rows and it never showed me the fifth one.

Can anyone help me? Or does anyone know any other way to have a lot of data in a single variable?


Thanks all, Frank
Reply With Quote
  #2 (permalink)  
Old 04-12-04, 12:05 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
use the dot operator..
PHP Code:

//note the dot before equal sign

$temp .='<p whatever'
this way, PHP will append the new text at the end of the variable ..
it's that simple
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 04-12-04, 01:11 PM
Frank Frank is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Montreal, Canada
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Hmm, it doesn't work, it adds even more text to the table (1,1,1,2,1,1,1,1,2, etc.)

Here's the full code of the loop:

PHP Code:

$news mysql_query('SELECT * FROM scorp_news',$db);

if (
$myrow mysql_fetch_array($news)) {
    do {
        
$temp .= '<p align="left"><b>[<u>'$myrow['id'] .'</u>]</b> 'nl2br($myrow['text']) .'</p>'.
        
$blabla .= $blabla $temp;

        } while (
$myrow mysql_fetch_array($news));
        
$text .= '<div align="center"><table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="20%" height="20%"><tr><td width="20%" height="20%" valign="top">'$blabla .'</td></tr></table></div>';
    } else {
        echo 
'cannot connect db';
        } 
By the way, this code is still a big draft.. I did try to modify the code to make it work in other ways but it always output a few times the same rows. Looks like it's always adding $blabla after $blabla itself, which doubles the data with the same data but it shouldn't do this! What do you think?


I would be glad if you could help me fixing this code
Thanks, Frank
Reply With Quote
  #4 (permalink)  
Old 04-12-04, 02:31 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Quote:
Originally Posted by Frank
Looks like it's always adding $blabla after $blabla itself, which doubles the data with the same data but it shouldn't do this! What do you think?
As NeverMind said, $foo .= $bar will append $bar to $foo, so if you wrote:

PHP Code:

$blabla .= $blabla $temp
It's the same as this:

PHP Code:

$blabla $blabla $blabla $temp
So what you want to do is this:

PHP Code:

$blabla .= $temp
Hope this helps.
__________________
Blavv =|
Reply With Quote
  #5 (permalink)  
Old 04-12-04, 02:53 PM
Frank Frank is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Montreal, Canada
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the informations Blaw, it clarified a few things in my head now

I did what you told me and it still does the same thing, here's the modified code:
PHP Code:

$news mysql_query('SELECT * FROM scorp_news ORDER BY id ASC',$db);

if (
$myrow mysql_fetch_array($news)) {
    do {
        
$temp '<p align="left"><b>[<u>'$myrow['id'] .'</u>]</b> 'nl2br($myrow['text']) .'</p>'.
        
$blabla .= $temp;
        } while (
$myrow mysql_fetch_array($news));
        
$text $blabla;
    } 
This code shows rows 1,2,1,3,1,2,1 in this order and then stops. It stops after 7 loops when there are 5 rows in the table and there is nothing in the code that tells it to stop after the seventh loop!


Anyone got a clue on this? Thanks, Frank
Reply With Quote
  #6 (permalink)  
Old 04-12-04, 05:48 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Why do you have period after '</p>' ? Isn't it supposed to be semi-colon?

Anyway, you can surely achieve what you're doing with your approach, but more common one would be to use while loop this way:

PHP Code:

<?php


$news 
mysql_query('SELECT * FROM scorp_news ORDER BY id ASC',$db);

// Initialize the output var.
$text '';

// Check if there is at least one row returned.
if (mysql_num_rows($news) > 0) {
    
// Yes, there is.
    
while ($myrow mysql_fetch_array($news)) {
        
// Repeat the following until there is no more row to fetch.
        
$text .= '<p align="left"><b>[<u>'$myrow['id'] .'</u>]</b> 'nl2br($myrow['text']) .'</p>';
    }
}


// In the end, you have $text that holds all the output from the database, formatted.
echo $text;

?>
In addition, you don't even need to check the num_rows if you are sure that there is always going to be 5 rows (or at least one row) returned from the query.

HTH.
__________________
Blavv =|
Reply With Quote
  #7 (permalink)  
Old 04-12-04, 05:58 PM
Frank Frank is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Montreal, Canada
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Errm, the period after '</p>' was a typo error, thanks for pointing it out.

I tried your code and it WORKS! Wow, I still don't know what was the problem with my code, I'll comment it out and take a look at it in the near future.


Thanks for the help everyone! Frank
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
CMS using text files. May use Flash as the interface. bchalker Script Requests 0 02-06-04 01:47 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Editing a text file. cjh_60 PHP 1 10-14-03 03:59 PM
displaying all duplicate records in my table. dsumpter PHP 6 09-02-03 10:54 AM


All times are GMT -5. The time now is 10:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.