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 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to concatenate my strings

    //please correct my logic. I need to make those variables $topicid, $topic, $sentid & $sent as arrays which I need to use them outside those local loops.

    Code:
    <?php
    require_once("includes/connection.php");
    	$topics = mysql_query("SELECT * FROM trendingtopics ORDER BY topicid") or die(mysql_error());
    	$num_rows_topics = mysql_num_rows($topics);
    		
    	if ($num_rows_topics < 1) {
    							echo "No topics";
    						} 
    		else { 
    				while($row_topics = mysql_fetch_array($topics)) 
    				{
    							$topicid = $row_topics['topicid'];
    							$topic = $row_topics['topic'];
    
    							//echo $topicid.'<br \>';
    							//echo $topic.'<br \>';
    				}
    			}
    	$sentences = mysql_query("SELECT * FROM sentences ORDER BY sentid") or die(mysql_error());
    	$num_rows_sentences = mysql_num_rows($sentences);
    		
    	if ($num_rows_sentences < 1) {
    							echo "No sentences";
    						} 
    		else { 
    				while($row_sentences = mysql_fetch_array($sentences)) 
    				{
    							$sentid = $row_sentences['sentid'];
    							$sent = $row_sentences['sent'];
    
    							//echo $sentid.'<br \>';
    							//echo $sent.'<br \>';
    				}
    			}
    Need to concatenate every $topic[] with every $sent[]. Please help me code better.

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php
        $dataArray 
    = array();
        require_once(
    "includes/connection.php");
        
    $topics mysql_query("SELECT * FROM trendingtopics ORDER BY topicid") or die(mysql_error());
        if(
    mysql_num_rows($topics)!=0)
        {
            
    $x=0;
            while(
    $row_topics mysql_fetch_array($topics)) 
            {
                
    $dataArray['topic_id'][$x] = $row_topics['topicid'];
                
    $dataArray['topic'][$x] = $row_topics['topic'];
                
    $x++;
            }
        }
        else
        {
            echo 
    "no topic";    
        }
        
        
    $sentences mysql_query("SELECT * FROM sentences ORDER BY sentid") or die(mysql_error());
        if(
    mysql_num_rows($sentences)!=0)
        { 
            
    $x=1;
            while(
    $row_sentences mysql_fetch_array($sentences)) 
            {
                
    $dataArray['sent_id'][$x] = $row_sentences['sentid'];
                
    $dataArray['sent'][$x] = $row_sentences['sent'];
                
    $x++;
            }
        }
        else
        {
            echo 
    "No sentences";

        }
    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    //Why do we have the counter $x=0 in first loop 
    $x=1 in second loop?
    
    What is the use of it.
    
    Also how do I use them outside those loops ...do some string function() on them?
    ?>

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    my bad, change it to 0. Does it work? didnt test it
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No phil its not working, something wrong while I am trying to call it...

    Code:
    <?php
    ...
    //I am facing problem in calling those two arrays outside their loops
    	
    foreach($dataArray=>topic as $eachTopic)
    		{
    
    		}
    ?>
    *Please check my previous post for my code...

  • #6
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    need some more correction, please help

    Code:
    <?php
        require_once("includes/connection.php");
    	
    	$dataArray1 = array();
    	$dataArray2 = array();   
    	$resultArray = array(); 
        $topics = mysql_query("SELECT * FROM trendingtopics ORDER BY topicid") or die(mysql_error());
        if(mysql_num_rows($topics)!=0)
        {
            $x=0;
            while($row_topics = mysql_fetch_array($topics)) 
            {
                $dataArray1['topic_id'][$x] = $row_topics['topicid'];
                $dataArray1['topic'][$x] = $row_topics['topic'];
                $x++;
            }
        }
        else
        {
            echo "no topic";    
        }
    
    
    
        $sentences = mysql_query("SELECT * FROM sentences ORDER BY sentid") or die(mysql_error());
        if(mysql_num_rows($sentences)!=0)
        { 
            $x=1;
            while($row_sentences = mysql_fetch_array($sentences)) 
            {
                $dataArray2['sent_id'][$x] = $row_sentences['sentid'];
                $dataArray2['sent'][$x] = $row_sentences['sent'];
                $x++;
            }
        }
        else
        {
            echo "No sentences";
    
        }
    
    
    	//I need to concatenate each topic with each sentence. My two foreach loops are not concatenating as expected... Need correction here
    
    	foreach($dataArray1['topic'] as $eachTopic)
    		{
    		foreach($dataArray2['sent'] as $eachSent)
    				{
    					$resultArray[] = $eachTopic." ".$eachSent;
    					print_r($resultArray);
    					echo "<br \>";
    				}
    				
    		}
    						
    						
    ?>
    My output is coming as

    Code:
    'Hi' ' ' 'Hello'                            //'topic1' 'space' 'sentence1'
    'Hi' ' ' 'Hello' ' ' 'This'                 //'topic1' 'space' 'sentence1' 'space' 'sentence2'
    'Hi' ' ' 'Hello' ' ' 'This' ' ' 'is'          //'topic1' 'space' 'sentence1' 'space' 'sentence2' 'space' 'sentence3'
    .......
    
    //My expected result
    'Hi' ' ' 'Hello'                       //'topic1' 'space' 'sentence1'
    'Hi' ' ' 'This'                       //'topic1' 'space' 'sentence2'
    'Hi' ' ' 'is'                          //'topic1' 'space' 'sentence3'
    
    'Bye' ' ' 'Hello'                   //'topic2' 'space' 'sentence1'
    'Bye' ' ' 'This'                    //'topic2' 'space' 'sentence2' 
    ...

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    try

    PHP Code:
    foreach($dataArray1['topic'] as $key=> $eachTopic)
    {
        echo 
    $eachTopic." ".$dataArray1['sent'][$key]."<br />";
                    

    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Done

    Code:
    <?php
    
    		for($y=0; $y <= $counter1; $y++)
    				{
    				for($z=0; $z <= $counter2; $z++)
    					{
    					$result = $dataArray1['topic'][$y]." ".$dataArray2['sent'][$z];
    echo $result;
    
    ?>

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    **claps**
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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