Jump to content

[SOLVED] my two tables


Recommended Posts

table1  :  col1 = topicid , col2 = topic

 

table2  :  col1 = sentid, col2 = sentence

 

require_once("includes/connection.php");
$trends = mysql_query("SELECT topicid, topic FROM table1");
$sent = mysql_query("SELECT sentid, sentence FROM table2");
//I need to concatenate every topic in table1
//with every sentence in table2 and create array of sentences.

 

help me with snippet please.

 

 

Link to comment
Share on other sites

SELECT CONCAT(topic, sentence) FROM table1, table2

 

done!

 

It does not give me any output apart from this..

 

Resource id #4

 

And I have lot of data in both the tables. Also I need to concatenate every topic with every sentence...eq

 

Hello , Hi , Bye
This is me, This is him

Hello This is me
Hello This is him
Hi This is me
Hi This is him
Bye This is me
Bye This is him

Link to comment
Share on other sites

That would seem to indicate that you are echoing the resource returned by a query, you have to use that resource to fetch the data:

 

Something along these lines (this is NOT real code, it WILL NOT run):

$DBconn = mysql_connect (Host, User, Password)
mysql_select_db (Database)
$Res = mysql_query(SELECT some stuff FROM somewhere WHERE i want it)
while ($Data = mysql_fetch_array($Res)) {
   print_r($Data)
}

Link to comment
Share on other sites

//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.

 

<?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.

Link to comment
Share on other sites

If you insist on using two separate queries, your logic should work with a couple of minor changes:

 

            while($row_topics = mysql_fetch_array($topics)) 
            {
                     $topicid = $row_topics['topicid'];
                     $topic = $row_topics['topic'];

                     //echo $topicid.'<br \>';
                     //echo $topic.'<br \>';
            }

 

you are assigning each row to the same variables, so in the end, the variables, $topicid and $topic, will contain only the last row from the database.  You can turn those into arrays by adding the square-brackets to them in the assignment:

 

            while($row_topics = mysql_fetch_array($topics)) 
            {
                     $topicid[] = $row_topics['topicid'];
                     $topic[] = $row_topics['topic'];
            }

 

you will need to do the same with the sentence loop later.

Link to comment
Share on other sites

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

 

<?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; 
?>

Link to comment
Share on other sites

Please correct my logic, I need to join each topic string with each string.

 

I've got a better idea. How about you read his suggestion and try to incorporate it into your code yourself?

 

Learning how to program is something that is difficult and takes time (if anyone told you otherwise they don't know what they're talking about, or they're lying). It comes from experience. However, experience does not come from copying other people's solutions that you don't understand.

 

Slow down and instead try to figure it out yourself instead of immediately asking for help :)

Link to comment
Share on other sites

...

Slow down and instead try to figure it out yourself instead of immediately asking for help :)

 

I agree with you Daniel, that we need to strive towards learning.

 

If only I could show you that I am working full time in learning and doing as much as I can in parallel.

 

You folks are experts, asking you folks while striving myself helps me learn to code better.

 

Please correct my logic, this one I could figure out... :shrug:

Link to comment
Share on other sites

   //I need to concatenate each topic with each sentence
   foreach($dataArray->topic as $eachTopic)
      {
         foreach(dataArray2->sent as $eachSent)
            {
               $resultArray[] = $eachTopic." ".$eachSent;
            }
      }
      
      echo $result; 

 

1) $dataArray is an array not an object.  The loop should be foreach($dataArray['topic'] as $eachTopic)

2) $dataArray2 should be handled in the same manner

3) 'echo $result' makes no sense because nothing was ever assigned to it.  The sentences are in $resultArray while will have to be echo'd in another for loop

 

Link to comment
Share on other sites

Thanks David... now some more correction please

 

<?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

 

'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' 
...

 

Link to comment
Share on other sites

I'm glad you got it.  And thanks for posting your solution.  The next person searching the forums for a similar situation may be able to use your solution to learn something.

 

Now, if you could mark the topic as solved, that would help others, too.  There's a tab at the bottom of the page you can click to mark it.  Only YOU can mark your topic as solved.

 

Good luck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.