I am using php html dom parser to gather information from 2 sites and when echoed it works just how i want it to but when i try and save some of my variables to a database it will only save the last result.
Here is the code i have been working on.
<?php
include_once('simple_html_dom.php');
ini_set('display_errors', true);
error_reporting(E_ALL);
$host="localhost";
$username="root";
$password="";
$database="betatv_local";
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$html = file_get_html('http://www.sidereel.com/Supernatural');
// find the title and echo it out
foreach($html->find('.title') as $title)
foreach($title->find('.sr-header') as $title2)
echo $title2->plaintext .'<br>';
echo '<br>';
// find the desc and echo it out
foreach($html->find('.summary') as $desc)
foreach($desc->find('.full-summary') as $descc)
echo $descc->plaintext .'<br>';
echo '<br>';
// find the showinfo and echo it out
foreach($html->find('.count') as $info)
echo $info->plaintext;
// find the airtime and echo it out
foreach($html->find('.airtime') as $air)
echo $air->plaintext;
// find the show status and echo it out
foreach($html->find('.status') as $status)
echo $status->plaintext .'<br>';
echo '<br>';
// find the show img and echo it out
foreach($html->find('.show-image') as $cover_img)
foreach($cover_img->find('img') as $cover_img_link)
echo $cover_img_link->src .'<br>';
echo '<br>';
$html = file_get_html('http://www.1channel.ch/watch-11054-Supernatural');
// find the show episodes and echo it out
foreach($html->find('.tv_container') as $episodes)
foreach($episodes->find('a') as $episode_links)
echo $episode_links->plaintext .'<br>';
$query = "INSERT INTO tv_shows (show_name,show_info,show_airs,show_status,show_cover,show_desc,show_episodes) VALUES('$title2->plaintext','$info->plaintext','$air->plaintext','$status->plaintext','$cover_img_link->src','$descc->plaintext','$episode_links->plaintext')";
mysql_query($query) or die(mysql_error());
// clean up memory
$html->clear();
unset($html);
?>
When echoed it looks like this.
Supernatural
Jensen Ackles and Jared Padalecki star as Dean and Sam Winchester, two
brothers searching for the meaning behind their mother’s death at the
hand of a demon, and their father’s mysterious disappearance.8 Seasons, 163 Episodes | Airs: 9:00 PM on The CW | Show Status:
Hiatushttp://s3.sidereel.com/tv_shows/4373/large/sc_supernatural.jpg
Season 1 Episode 1 – Pilot Episode 2 – Wendigo Episode 3 – Dead In The
Water Episode 4 – Phantom Traveler Episode 5 – Bloody Mary Episode 6 –
Skin Episode 7 – Hook Man Episode 8 – Bugs Episode 9 – Home Episode 10
– Asylum Episode 11 – Scarecrow Episode 12 – Faith Episode 13 – Route 666 Episode 14 – Nightmare Episode 15 – The Benders Episode 16 –
Shadow Episode 17 – Hell House Episode 18 – Something Wicked Episode
19 – Provenance Episode 20 – Dead Man’s Blood Episode 21 – Salvation
Episode 22 – Devil’s Trap Season 2 Episode 1 – In My Time of Dying
Episode 2 – Everybody Loves A Clown Episode 3 – Bloodlust Episode 4 –
Children Shouldn’t Play With Dead Things Episode 5 – Simon Said
Episode 6 – No Exit Episode 7 – The Usual Suspects Episode 8 –
Crossroad Blues Episode 9 – Croatoan Episode 10 – Hunted Episode 11 –
Playthings Episode 12 – Nightshifter Episode 13 – Houses of the Holy
Episode 14 – Born Under A Bad Sign Episode 15 – Tall Tales Episode 16
– Roadkill Episode 17 – Heart Episode 18 – Hollywood Babylon Episode 19 – Folsom Prison Blues Episode 20 – What Is and What Should Never Be
Episode 21 – All Hell Breaks Loose, Part 1 Episode 22 – All Hell
Breaks Loose, Part 2 Season 3 Episode 0 – Promo Episode 1 – The
Magnificent Seven Episode 2 – The Kids Are Alright Episode 3 – Bad Day
at Black Rock Episode 4 – Sin City Episode 5 – Bedtime Stories Episode
6 – Red Sky At Morning Episode 7 – Fresh Blood Episode 8 – A Very
Supernatural Christmas Episode 9 – Malleus Maleficarum Episode 10 –
Dream A Little Dream Of Me Episode 11 – Mystery Spot Episode 12 – Jus
In Bello Episode 13 – Ghostfacers Episode 14 – Long-Distance Call
Episode 15 – Time Is On My Side Episode 16 – No Rest For The Wicked
Season 4 Episode 0 – Promo Episode 1 – Lazarus Rising Episode 2 – Are
You There God? It’s Me, Dean Winchester Episode 3 – In the Beginning
Episode 4 – Metamorphosis Episode 5 – Monster Movie Episode 6 – Yellow
Fever Episode 7 – It’s the Great Pumpkin, Sam Winchester Episode 8 –
Wishful Thinking Episode 9 – I Know What You Did Last Summer Episode
10 – Heaven and Hell Episode 11 – Family Remains Episode 12 – Criss
Angel Is a Douchebag Episode 13 – After School Special Episode 14 –
Sex and Violence Episode 15 – Death Takes a Holiday Episode 16 – On
the Head of a Pin Episode 17 – It’s a Terrible Life Episode 18 – The
Monster at the End of This Book Episode 19 – Jump the Shark Episode 20
– The Rapture Episode 21 – When the Levee Breaks Episode 22 – Lucifer Rising Season 5 Episode 0 – Promo Episode 1 – Sympathy for the Devil
Episode 2 – Good God, Y’all! Episode 3 – Free to Be You and Me Episode
4 – The End Episode 5 – Fallen Idol Episode 6 – I Believe the Children
Are Our Future Episode 7 – The Curious Case of Dean Winchester Episode
8 – Changing Channels Episode 9 – The Real Ghostbusters Episode 10 –
Abandon All Hope… Episode 11 – Sam, Interrupted Episode 12 – TBA
Episode 13 – Back to the Future II Episode 14 – My Bloody Valentine
Episode 15 – Dead Men Don’t Wear Plaid Episode 16 – Dark Side of the
Moon Episode 17 – 99 Problems Episode 18 – Point of No Return Episode
19 – Hammer of the Gods Episode 20 – The Devil You Know Episode 21 –
Two Minutes to Midnight Episode 22 – Swan Song Season 6 Episode 0 –
Promo Episode 1 – Exile On Main St. Episode 2 – Two and a Half Men
Episode 3 – The Third Man Episode 4 – Weekend at Bobby’s Episode 5 –
Live Free or Twihard Episode 6 – You Can’t Handle the Truth Episode 7
– Family Matters Episode 8 – All Dogs Go to Heaven Episode 9 – Clap Your Hands If You Believe Episode 10 – Caged Heat Episode 11 –
Appointment in Samarra Episode 12 – Like a Virgin Episode 13 –
Unforgiven Episode 14 – Mannequin 3: The Reckoning Episode 15 – The
French Mistake Episode 16 – …And Then There Were None Episode 17 –
My Heart Will Go On Episode 18 – Frontierland Episode 19 – Mommy
Dearest Episode 20 – The Man Who Would Be King Episode 21 – Let It
Bleed Episode 22 – The Man Who Knew Too Much Season 7 Episode 0 –
Promo Episode 1 – Meet the New Boss Episode 2 – Hello Cruel World
Episode 3 – The Girl Next Door Episode 4 – Defending Your Life Episode
5 – Shut Up, Dr. Phil Episode 6 – Slash Fiction Episode 7 – The
Mentalists Episode 8 – Time for a Wedding! Episode 9 – How to Win
Friends and Influence Monsters Episode 10 – Death’s Door Episode 11 –
Adventures in Babysitting Episode 12 – Time After Time Episode 13 –
The Slice Girls Episode 14 – Plucky Pennywhistle’s Magic Menagerie
Episode 15 – Repo Man Episode 16 – Out With the Old Episode 17 – The
Born-Again Identity Episode 18 Episode 19 Episode 20 – The Girl With
the Dungeons and Dragons Tattoo Episode 21 – Reading Is Fundamental
Episode 22 – There Will Be Blood Episode 23 – Survival of the Fittest
Season 8 Episode 0 – Season 8 Promo Episode 1 – We Need to Talk About
Kevin Episode 2 – What’s Up, Tiger Mommy? Episode 3 – Heartache
Episode 4 – Bitten Episode 5 – Blood Brother Episode 6 – Southern
Comfort Episode 7 – A Little Slice of Kevin Episode 8 – Hunteri
Heroici Episode 9 – Citizen Fang Episode 10 – Torn and Frayed Episode
11 – LARP and the Real Girl Episode 12 – As Time Goes By Episode 19 –
Of Grave Importance
Everything saves into the database fine except for my variable $episode_links. It only saves
As Time Goes By Episode 19 – Of Grave Importance
I need it to save all the information in the variable not just the last result. Any idea what im doing wrong here?
The php
foreachconstruct executes the next statement for each item in the array. If you want more than one thing to happen in the foreach (like echoing and saving to a database), you need to surround everything you want to happen with curly braces{}.Edited to append each episode to a variable as it’s parsing, and then insert that into the database.