help me code better

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

help me code better

Post by oneofthelions »

//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: Select all

<?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.
myasirm
Forum Commoner
Posts: 54
Joined: Sat Sep 12, 2009 3:57 am

Re: help me code better

Post by myasirm »

use this to concatenate two variables.
$topic"."$sent
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: help me code better

Post by AbraCadaver »

Here's two ways to do it:

Code: Select all

$i = 0;
while($row_topics = mysql_fetch_array($topics))
{
    $topicid[$i] = $row_topics['topicid'];
    $topic[$i] = $row_topics['topic'];
 
    //echo $topicid[$i].'<br \>';
    //echo $topic[$i].'<br \>';
    $i++;
}
--or--

Code: Select all

while($row_topics = mysql_fetch_array($topics))
{
    $topicid = $topicid[] = $row_topics['topicid'];
    $topic = $topic[] = $row_topics['topic'];
 
    //echo $topicid.'<br \>';
    //echo $topic.'<br \>';
}
Either way you have a $topic array and a $topicid array.

Or if you want an array indexed on the topicid with a value of topic, then:

Code: Select all

while($row_topics = mysql_fetch_array($topics))
{
    $topic[$row_topics['topicid']] = $row_topics['topic'];
    //echo $topicid.'<br \>';
    //echo $topic.'<br \>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: help me code better

Post by oneofthelions »

This is what I got. Please correct my logic, I need to join each topic string with each string.

Code: Select all

 
<?php
    $dataArray = array();
    $dataArray2 = array();
    $resultArray = 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)) 
        {
            $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
    foreach($dataArray->topic as $eachTopic)
        {
            foreach(dataArray2->sent as $eachSent)
                {
                    $resultArray[] = $eachTopic." ".$eachSent;
                }
        }
        
        echo $result; 
?>
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: help me code better

Post by oneofthelions »

Thanks David... now some more correction please

Code: Select all

 
<?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: Select all

 
'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' 
...
 
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: help me code better

Post by oneofthelions »

Compelte!!

Code: Select all

        for($y=0; $y <= $counter1; $y++)
                {
                for($z=0; $z <= $counter2; $z++)
                    {
                    $result = $dataArray1['topic'][$y]." ".$dataArray2['sent'][$z];
}}
Post Reply