salman_ahad@yahoo.com Posted October 28, 2009 Share Posted October 28, 2009 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. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 28, 2009 Share Posted October 28, 2009 SELECT CONCAT(topic, sentence) FROM table1, table2 done! Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 28, 2009 Author Share Posted October 28, 2009 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 Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 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) } Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 29, 2009 Author Share Posted October 29, 2009 //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. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 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. Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 29, 2009 Author Share Posted October 29, 2009 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; ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 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 Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 29, 2009 Author Share Posted October 29, 2009 ... 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... Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 any help is appreciated Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 30, 2009 Share Posted October 30, 2009 //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 Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 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' ... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 30, 2009 Share Posted October 30, 2009 Seriously, make an effort yourself. If you want people to do things for you then post in the freelancing section. Quote Link to comment Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 30, 2009 Author Share Posted October 30, 2009 DID IT!! for($y=0; $y <= $counter1; $y++) { for($z=0; $z <= $counter2; $z++){ $result = $dataArray1['topic'][$y]." ".$dataArray2['sent'][$z]; echo $result; echo "<br \>"; } } Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 30, 2009 Share Posted October 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.