Jump to content

How to combine these two outputs as one


chauhanRohit

Recommended Posts

This is the code

$query = mysql_query("SELECT fname,lname FROM structure.authors ");
$query1 = mysql_query("SELECT book_name FROM structure.books");
    $count = mysql_num_rows($query);
    $count1 = mysql_num_rows($query1);
       
	    if($count ==0 and $count1 ==0  ){
	        $output = '<div>no results</div>';
	      }else{ 
	     
		    $output = '<div>';	
	           while($row = mysql_fetch_array($query)){
		       $output .='<div> 
			          '.$row['fname'].'  '.$row['lname'].' 
					  </div>';
			  	      }
	              $output .='<div>';
	  
	        //the output from second table           
			   $output1 = '<div>';	
	           while($row = mysql_fetch_array($query1)){
		       $output1 .='<div> 
			          '.$row['book_name'].' 
					  </div>';
			 		 
			 }
	         $output1 .='<div>';
	  }
echo $output;
echo $output1;

Link to comment
https://forums.phpfreaks.com/topic/288174-how-to-combine-these-two-outputs-as-one/
Share on other sites

programming requires that you define what data you have as inputs to your code and what output you want to produce from that data.

 

all you have stated is you want to combine two outputs. you haven't shown or stated what output you want or what is wrong with the output your code currently produces.

 

if you post some example data from your two tables, 2 or 3 rows each would be enough, and show what exact output you want to produce for that example data, someone can probably help you.

This is the output that i get when i a use this code, I have 2 tables authors and books and i want to display the author names and book names from the two tables

$query = mysql_query("SELECT authors.fname,authors.lname,books.book_name from authors,books ") or die ('cannot connect');
  $count = mysql_num_rows($query);
        if($count ==0){
	        $output = 'no results';
	      }else{ 
		    $output = '<div>';	
	           while($row = mysql_fetch_array($query)){
				   $output .='<div> 
						  '.$row['fname'].'  '.$row['lname'].'</br> 
							'.$row['book_name'].' </br></br>
						    </div>';
						  }
					  $output .='<div>';
		  
		  }
		  echo $output;

THE output

sydney sheldon
If Tomorrow Comes
Jeffrey archer
If Tomorrow Comes
Ruskin Bond
Chetan Bhagat
If Tomorrow Comes
sydney sheldon
Master of the Game
Jeffrey archer
Master of the Game
Ruskin Bond
Master of the Game
Chetan Bhagat

 

The output that i want is

sydney sheldon
Jeffrey archer
Ruskin Bond
Chetan Bhagat

If Tomorrow Comes

Master of the Game

you have now switched your code to use a query that's join'ing every row from the author table with every row from the book table.

 

at this point, i don't think we can help you. all you are doing in this thread and in the thread in the other help site is to randomly change things without any reason behind it. you were also told in the other help site that if there's no relationship between the two pieces of output, that you need to use two queries.

hey the first code i posted is giving me the result that i want but as i said earlier it has two outputs, so i tried editing the code and posted the new code here, i dont know why this annoyed you guys i am just looking for a solution here.

 

if you see i have managed to get the results as one  outputs in my second code , but the results are being duplicated can you just  tell me a way to avoid this.

 

as per the relationship between the two tables   i have added a foreign key in the books table (author_id), all i want is the names and the book names from the two tables.

 

 

if this not possible , can u suggest a way to achieve this.

The very last attempt:

 

First of all, you need two queries. Not one. Two. One query for the authors, one for the books. Just like in your first post. OK?

 

Now comes the “magic”: Instead of putting the authors into $output and the books into $output1 (which logically leads to “two outputs”), you put them all into $output. So you have one variable. Not two. One. OK?

 

So the result sets of two queries go into one variable, just like you wanted. Not sure why this is so hard to understand.

Now comes the “magic”: Instead of putting the authors into $output and the books into $output1 (which logically leads to “two outputs”), you put them all into $output. So you have one variable. Not two. One. OK?

 

So the result sets of two queries go into one variable, just like you wanted. Not sure why this is so hard to understand.

Thanks man this helped .

finally

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.