Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay data of one table based on Id of other using SQL, PHP (RSS Style)
    primarykey
    data
    text
    <p>probably a straightforward to the trained eye but I have agonised over this for too long now..</p> <p>I have two db tables designed to generate RSS feeds to the browser: 'Channel' and 'Item':</p> <hr> <pre><code>**Channel Table** Primary Key: Id Id Title Desc Link 1 News latest www... 2 Sport latest www... 3 Gossip latest www.. </code></pre> <p>As you can see there is more than one channel, which is central to my query..</p> <pre><code>**Item Table** Primary Key: Id, Index Key: Chan_Id Id Title Desc Link Chan_Id 1 Footie Liv-ManU1 www... 2 2 Cricket India-Eng.. www... 2 3 G5 Summit G5 Talks.. www... 1 4 X-Factor Simon Cowell.www... 3 5 Iraq MOD says... www... 1 6 M Jackson Court trial..www... 3 </code></pre> <hr> <p>Now I have seen scripts where an RSS feed can be displayed thru PHP and it works great - the main problem is all examples I have seen assumes you have only One channel to attach your items to..</p> <pre><code>News G5 Summit G5 Talks...... Iraq MOD says...... </code></pre> <p>seeing as I have more than one channel, what I am looking for is to first print out multiple channels with related content:</p> <pre><code>News G5 Summit G5 Talks...... Iraq MOD says...... Sport Footie Liv-ManU1..... Cricket India-Eng..... Gossip X-Factor Simon Cowell.. M Jackson Court trial.. </code></pre> <p>then ultimately dump this data into separate xml files, depending on subject matter (Footie.xml, Cricket.xml, etc.) This part just needs a little fine tuning, as I have a formula for this using PHP DOM to construct and save:</p> <pre><code>$doc-&gt;save(row['chan_title']) </code></pre> <p>not the exact syntax but method I use works, but shows only channels NOT items. The central issue is two-fold:</p> <ol> <li><p>SQL Query: how would I go about selecting the data from the db, as a join? If so, an Inner Join similar to this:</p> <p>SELECT Channel.Id, Channel.Title, Item.Title, Item.Desc, Item.Chan_Id FROM Channel INNER JOIN Item ON Channel.Id=Item.Chan_Id ORDER BY Channel.Id</p></li> </ol> <p>or maybe use a sub-query?</p> <ol> <li>How do I then get the data from two tables and display it with PHP? Would it be easier to wrap up methods in classes and do it that way? I've tried to use a <code>while( channel query)</code> inside <code>while( item query)</code> inside <code>foreach( item)</code> but only displayed channel info. Possibly the wrong permutation but I've been experimenting with a view of finding a solution but need some expert help so I can move on!!</li> </ol> <p>Your help will be appreciated, I will try and respond as soon as I can.</p> <p><strong>EDIT - Wow, 2 days and no response, can't be that hard!!</strong> Well actually, it isn't. I didn't need it to be so complicated, the main point was more about the correct syntax. Still not found a true solution but getting close - I decided to use two queries, with two <code>while</code> statements, one inside the other:</p> <pre><code>$detQry = sprintf("SELECT * FROM ".$detailsTable); $itQry = sprintf("SELECT id, title, description FROM ".$itemsTable." WHERE chan_id = ".$detailsTable.".id ORDER BY chan_id ASC LIMIT 0, 30 "); $result = mysql_query($detQry) OR die ('Could not execute query: ' . mysql_error()); $run = mysql_query($itQry) OR die ('Could not execute query: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['title'].'&lt;br&gt;'; echo $row['description'].'&lt;br&gt;&lt;br&gt;'; while(list($ttl, $dsc, $pbd) = mysql_fetch_array($run)) { echo $ttl.'&lt;br&gt;'; echo $dsc.'&lt;br&gt;&lt;br&gt;'; echo $pbd.'&lt;br&gt;&lt;br&gt;&lt;br&gt;'; } } </code></pre> <p>Problem is, when I run this script only the Channel 'title ' and 'description' print.. what am I <strong>not</strong> doing to make this work? Could it be to do with the <code>list()</code> statement?</p> <p>Any help welcomed to shorten the agony...</p> <p><strong>EDIT 2 - Resolved, thanks to a suggestion from Woody.</strong> It took a long time, but finally got the specific result I was looking for after I tested the formula given by Woody. There are a few One Channel to 'n' Items scripts, but I needed a 'n' Channel to 'n' Items - this is perfect and flexible. With my current level of experience it would've taken a long time to come up with the answer like this - thanks again. <pre><code>(some db_connect stuff..) $query = "SELECT * FROM webref_rss_items, webref_rss_details WHERE webref_rss_details.id = webref_rss_items.chan_id ORDER BY webref_rss_details.id"; $t_result = mysql_query( $query ) OR die ('Could not execute query: ' . mysql_error()); $rowCount = mysql_num_rows( $t_result ); $cat = -1; // last category for($r = 0; $r &lt; $rowCount; $r++) { $row = mysql_fetch_array( $t_result ); if($row['id'] != $cat) { $cat = $row['id']; //create doctype $doc = new DOMDocument('1.0','UTF-8'); header('content-type: text/xml'); //create rss root with values $root = $doc-&gt;createElement('rss'); $root-&gt;setAttribute('version', '2.0'); $doc-&gt;appendChild($root); //create channel element $channel = $doc-&gt;createElement("channel"); $root-&gt;appendChild($channel); // add node for each record, create the text nodes for element and add text $title = $doc-&gt;createElement('title', $row['title']); $channel-&gt;appendChild($title); $link = $doc-&gt;createElement('link', $row['link']); $channel-&gt;appendChild($link); $desc = $doc-&gt;createElement('description', $row['description']); $channel-&gt;appendChild($desc); $lang = $doc-&gt;createElement('language', $row['lang']); $channel-&gt;appendChild($lang); $image = $doc-&gt;createElement('image'); $image = $channel-&gt;appendChild($image); $imttl = $doc-&gt;createElement('title', $row['image_title']); $image-&gt;appendChild($imttl); $imlink = $doc-&gt;createElement('link', $row['image_link']); $image-&gt;appendChild($imlink); $imdesc = $doc-&gt;createElement('description', $row['image_desc']); $image-&gt;appendChild($imdesc); $imwidth = $doc-&gt;createElement('width', $row['image_width']); $image-&gt;appendChild($imwidth); $imheight = $doc-&gt;createElement('height', $row['image_height']); $image-&gt;appendChild($imheight); $imurl = $doc-&gt;createElement('url', $row['image_url']); $image-&gt;appendChild($imurl); $manEdit = $doc-&gt;createElement('managingEditor', $row['man_edit']); $channel-&gt;appendChild($manEdit); $webmaster = $doc-&gt;createElement('webmaster', $row['webmaster']); $channel-&gt;appendChild($webmaster); $copyright = $doc-&gt;createElement('copyright', $row['copyright']); $channel-&gt;appendChild($copyright); $pubDate = $doc-&gt;createElement('pubDate', $row['ch_pubDate']); $channel-&gt;appendChild($pubDate); $lastBuild = $doc-&gt;createElement('lastBuildDate', $row['lastBuild']); $channel-&gt;appendChild($lastBuild); $category = $doc-&gt;createElement('category', $row['category']); $channel-&gt;appendChild($category); $generator = $doc-&gt;createElement('generator', $row['generator']); $channel-&gt;appendChild($generator); $docs = $doc-&gt;createElement('docs', $row['docs']); $channel-&gt;appendChild($docs); $cloud = $doc-&gt;createElement('cloud', $row['cloud']); $channel-&gt;appendChild($cloud); $ttl = $doc-&gt;createElement('ttl', $row['ttl']); $channel-&gt;appendChild($ttl); $rating = $doc-&gt;createElement('rating', $row['rating']); $channel-&gt;appendChild($rating); $textInput = $doc-&gt;createElement('textInput', $row['textInput']); $channel-&gt;appendChild($textInput); $skipHours = $doc-&gt;createElement('skipHours', $row['skipHours']); $channel-&gt;appendChild($skipHours); $skipDays = $doc-&gt;createElement('skipDays', $row['skipDays']); $channel-&gt;appendChild($skipDays); } //Dynamically Generated Items $item = $doc-&gt;createElement('item'); $item = $channel-&gt;appendChild($item); $it_ttl = $doc-&gt;createElement('title', $line['title']); $item-&gt;appendChild($it_ttl); $it_desc = $doc-&gt;createElement('description', $line['desc']); $item-&gt;appendChild($it_desc); $it_link = $doc-&gt;createElement('link, $line['link']'); $item-&gt;appendChild($it_link); $it_guid = $doc-&gt;createElement('guid', $line['guid']); $item-&gt;appendChild($it_guid); $it_pubDt = $doc-&gt;createElement('pubDate', $line['pubDate']); $item-&gt;appendChild($it_pubDt); $it_auth = $doc-&gt;createElement('author', $line['author']); $item-&gt;appendChild($it_auth); $it_ctg = $doc-&gt;createElement('category', $line['category']); $item-&gt;appendChild($it_ctg); $it_cmnt = $doc-&gt;createElement('comments', $line['comments']); $item-&gt;appendChild($it_cmnt); $it_encl = $doc-&gt;createElement('enclosure', $line['enclosure']); $item-&gt;appendChild($it_encl); $it_src = $doc-&gt;createElement('source', $line['source']); $item-&gt;appendChild($it_src); $mast = $row['ch_title']; $saw = explode(" ", $mast); $chip = $saw[0].'-'.$saw[1]; // saw1-saw2 $beam = "../../feed/".$chip.".xml"; echo 'Wrote: ' . $doc-&gt;save($beam) . ' bytes. &lt;br /&gt;&lt;br /&gt;'; } } ?&gt; </code></pre> <p>Works perfectly!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload