oneofthelion
10-29-2009, 06:50 AM
//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.
Phil Jackson
10-29-2009, 07:42 AM
<?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";
}
?>
oneofthelion
10-29-2009, 10:46 PM
<?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?
?>
Phil Jackson
10-29-2009, 11:07 PM
my bad, change it to 0. Does it work? didnt test it
oneofthelion
10-30-2009, 12:47 AM
No phil its not working, something wrong while I am trying to call it...
<?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...
oneofthelion
10-30-2009, 06:37 AM
need some more correction, please help
<?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'
...
Phil Jackson
10-30-2009, 07:48 AM
try
foreach($dataArray1['topic'] as $key=> $eachTopic)
{
echo $eachTopic." ".$dataArray1['sent'][$key]."<br />";
}
oneofthelion
10-30-2009, 07:40 PM
Done
<?php
for($y=0; $y <= $counter1; $y++)
{
for($z=0; $z <= $counter2; $z++)
{
$result = $dataArray1['topic'][$y]." ".$dataArray2['sent'][$z];
echo $result;
?>