So whenever I refresh the page, the error changes. Here are a few of the results:
Fatal error: Cannot redeclare class db in on line 58
Fatal error: Cannot redeclare class db in Unknown on line 0
Fatal error: Cannot redeclare class db in fj on line 58
Fatal error: Cannot redeclare class db in on line 11
Fatal error: Cannot redeclare class db in ��� on line 58
I have one main class, which contains every other class so I can easily access any class from anywhere.
Here's main.php (the main class):
<?php
class main
{
private $db, $web, $fj;
public function __construct(){
require "inc/db.class.php";
require "inc/web.class.php";
require "inc/fj.class.php";
$this->db = new db();
$this->web = new web();
$this->fj = new fj();
}
public function web(){
return $this->web;
}
public function fj(){
return $this->fj;
}
public function db(){
return $this->db;
}
}
?>
here's the fj class:
<?php
class fj extends main
{
/*
* updateTopImages(int)
* updates the top images and database until 50 pages. Starts on the id given. Recursive.
* returns true on success, false on fail
*/
public function updateTopImages($id){
/* load up our variables */
require "inc/parse.lib.php"; // Load our parse library
$page = "http://www.funnyjunk.com/funny_pictures/pages/picturetop24h/asc/" . $id;
$source = parent::web()->getSource($page);
$dom = new DOMDocument();
@$dom->loadHTML($source); // Silenced because of bad html markup
$divs = $dom->getElementsByTagName('div');
/* cycle through divs until main is found and get all pictures */
foreach($divs as $div){
if($div->hasAttribute("class")){
if($div->getAttribute("class") == "main"){
$pics = $div->getElementsByTagName("li");
}
}
}
/* cycle through our pictures and strip data */
$final = array();
foreach($pics as $index => $pic){
$fjid = str_replace("/funny_pictures/", "", $href);
$fjid = substr($href, 0,strpos($href, "/"));
$this->updateImage($fjid);
}
}
/*
* getImageInfo(int)
* Strips information from that image. Doesn't save or anything.
* returns array of info
*/
public function getImageInfo($id){
require "inc/parse.lib.php";
$page = "http://www.funnyjunk.com/funny_pictures/" . $id;
$source = parent::web()->getSource($page);
$dom = new DOMDocument();
@$dom->loadHTML($source); // Silenced because of bad html markup
$final = array();
$final['title'] = $dom->find("title")->item(0)->nodeValue;
die($final['title']);
}
/*
* updateImage(int, string())
* updates information on that image. Insert picture into database if picture not found.
* returns true on success, false on fail
*/
public function updateImage($id, $extra=array()){
$mysqli = parent::db()->connect();
if($stmt = $mysqli->prepare("SELECT * FROM pictures WHERE fj_id=?")){
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows != 0){
if($stmt = $mysqli->prepare("UPDATE pictures SET title=?, description=?, thumbs=? WHERE fj_id=? LIMIT 1")){
$stmt->bind_param("sssi", $title, $description, $thumbs, $id);
$stmt->execute();
$stmt->store_result();
if($stmt->affected_rows == 1){
return true;
} else {
return false;
}
} else {
die("Update image failed: " . $mysqli->error);
}
} else {
if($stmt = $mysqli->prepare("INSERT INTO pictures(title, description, uploader, thumbs, picture)")){
} else {
die("Update image failed: " . $mysqli->error);
}
}
} else {
die("Update image failed: " . $mysqli->error);
}
}
}
?>
(this is still a work in progress)
The web class:
<?php
/*
* web.class.php
* Handles cURL stuff such as posting data or downloading sources
*/
class web
{
/*
* getSource(string)
* returns string
* Gets the source of a page
*/
public function getSource($page){
return $this->curl($page, array(CURLOPT_FRESH_CONNECT => true, CURLOPT_RETURNTRANSFER => true));
}
/*
* postData(string, string[])
* returns string
* Sends post data to the page and return the source
*/
public function postData($page, $data){
$fdata = "";
foreach($data as $x => $y){
if($fdata != ""){
$fdata .= "&";
}
$fdata .= urlencode($x) . '=' . urlencode($y);
}
return $this->curl($page, array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => $fdata, CURLOPT_FRESH_CONNECT => true, CURLOPT_RETURNTRANSFER => true));
}
/*
* curl(string, string[])
* returns string
* Performs a cURL operation on the page using the options and returns the result
*/
public function curl($page, $options){
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $page);
curl_setopt_array($c, $options);
$response = curl_exec ($c);
curl_close ($c);
return $response;
}
}
?>
and the db class:
<?php
class db extends main
{
public function connect(){
return new mysqli("localhost", "root", "******", "fj");
}
}
?>
No idea what's going on or why it's throwing this error. This has to be the weirdest thing to happen to me with PHP.
This post has been edited by creativecoding: 26 November 2011 - 03:03 AM

New Topic/Question
Reply





MultiQuote







|