Jump to content

help with extracting data into mysql and refreshing url.


caine

Recommended Posts

I had few problems with my code. The problems are:
(i) unable to store data into mysql database table including current date publications
(ii) having problem programming the refreshing url part for data updating

I would like to gain assistance with these. Can anybody help with these? I'm working on extracting news/bulletin from the $URL and store into database "bulletin" with table name "bul_data". The html source file can be found from the $URL.

<?php


$URL="http://bulletin.mmu.edu.my/v3/view/bytodaytitle.mmu";

$f = fopen($URL, "r");
  if($f)
        while($pre = fread($f, 1000))
$source = $source.$pre;
    else
    {
        echo 'Unable to open '.$URL.'.';
        die;
    }
   
        include 'connect.php';


//extract the date into database
//$date_string= substr_count($source, "<td width=\"100%\" colspan=\"4\">");
//$date1 = strpos($source, "<td width=\"100%\" colspan=\"4\"><font face=\"Verdana, Arial\" size=\"1\"
//                          color=\"#000066\">List of Today's Bulletin : <b>", 0);
//$date_end1 = strpos($source, "</b></font></td>", $date1);
//$date1 = $date1 + strlen($date1);
//$date = substr($source, $date1, $date_end1- $date1;);
//$date = check($date);

//@mysql_query("INSERT INTO `bul_data` VALUES ('$date')", $db);
 
$total= substr_count($source, "<td width=\"20%\" >");

//extract necessary information into database
$pos=0;
for($counter=0;$counter<$total;$counter++)
{
$line1 = strpos($source, "<td width=\"20%\" ><font face=\"Verdana, Arial\" size=\"1\">", $pos);
$end1 = strpos($source, "</a></td></font>", $begin1);
$line1 = $line1 + strlen($line1);
$end1 = $end1 - $line1;
$department = substr($source, $line1, $end1);
$department = check($department);

$line2 = $line1 + $end1 + 1;
$line2 = strpos($source, "<a href=\"", $line2);
$end2 = strpos($source , "\" target=\"main\"" , $line2);
$line2 = $line2 + strlen($line2);
$end2 = $end2 - $line2;
$link = substr($source , $line2, $end2);
$link = check($link);

$line3 = $line2 + $end2 + 1;
$line3 = strpos($source , "onMouseOut=\"b()\">" , $begin3);
$end3 = strpos($source , "</font></a></td>" , $begin3);
$line3 = $line3 + strlen($line3);
$ended = $end3 - $line3;
$title = substr($source , $line3 , $ended);
$title = check($title);

$line4 = $line3 + $ended + 1;
$line4 = strpos($source , "<td width=\"17%\" align=\"center\"><font face=\"Verdana, Arial\" size=\"1\">" , $begin4);
$end4 = strpos($source , "</td></font>" , $begin4);
$line4 = $line4 + strlen($line4);
$end4 = $end4 - $line4;
$campus = substr($source , $begin4 ,$line4);
$campus = check($campus);

$pos = $line4 + $end4 + 1;

@mysql_query("INSERT INTO `bul_data` VALUES ('$title', '$department', '$campus', '$link')", $db);

}

function check($string)
{

$string = htmlspecialchars($string, ENT_QUOTES);
//$string = str_replace("&", "&amp;", $string);
//$string = str_replace("<", "&lt;", $string);
//$string = str_replace(">", "&gt;", $string);
//$string = str_replace("'", "&apos;", $string);
//$string = str_replace("\"", "&quot;", $string);
return $string;
}

***********//problem with this refreshing part for information update*****************
//function ripnew($URL)
{
$newURL="http://bulletin.mmu.edu.my/v3/view/$URL";
$f = fopen($newURL, "r");
  if($f)
        while($pre = fread($f, 1000))
$source = $source.$pre;
    else
    {
        echo 'Unable to open '.$URL.'.';
        return false;
}
 
$begin =  strpos ($source,"</table>", 0);
$begin = $begin + 8;
$end = strpos($source,"</body>",$begin);
$end = $end - $begin;
$detail = substr($source, $begin, $end);

return $detail;
}

?>

This is the connect.php file:

<html>
<head>
<title></title>
</head>
<body>

<?php
$host="localhost";
$username="root"; //unknown
$password=""; //unknown
$dbname="bulletin";

$db = mysql_connect("$host", "$username", "$password") or die("Unable to connect");

      mysql_select_db("$dbname") or die(mysql_error());
?>
</body>
<html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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