Home >Backend Development >PHP Tutorial >dedecms batch extracts the code of the first picture as the thumbnail (article + software)_PHP tutorial

dedecms batch extracts the code of the first picture as the thumbnail (article + software)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:27910browse

The specific implementation code is as follows: A brief explanation will be given below.

Copy code The code is as follows:

/*
1. Configure your Database connection
2. Note that the prefix of the data table name defaults to dede_
3. Be sure to back up your database first or back up the dede_archives table. I am not responsible for any losses.
*/
//Database Connection settings
$ip = "localhost"; //Address
$user = "jb51"; //Username
$pw = "www.jb51.net"; //Password
$ db = "jb51"; //Table name
$conn=mysql_connect ($ip,$user,$pw);
mysql_select_db($db);
mysql_query("set names gbk"); / /Set encoding to GBK

//Start! Get body data
function body($id){
$exec="select * from dede_addonsoft where aid = '".$id."' ";
$result=mysql_query($exec);
$rs=mysql_fetch_object($result);
$id = $rs->introduce;
return $id =$id;
}
//End! Get body data

//Start! Extract the first image address in the variable
function bodyimg($obj) {

if(isset ($obj)){
if ( preg_match( "", $obj, $regs ) ) { //Use Regularly obtain the first image address
return $obj = $regs[1] ;
} else {
echo "Erro: No image address!
";
return $obj = 1 ;
}
}


}
//End! Extract the first image address in the variable

//Start modifying rchives and insert the obtained Image address
function changearchives($pic,$id) {
$sql="UPDATE `dede_archives`
SET `litpic` = '".$pic."'
WHERE `id` = '".$id."'";
$result=mysql_query($sql);
if($result==1) echo "Data modification successful!
";
else echo "Failed to modify data!
";
}
//End modifying rchives, insert the obtained image address

//Display data Table content
$exec="select * from dede_archives where typeid in(494,495,496,497,498,499,500,501,502,503,504,374,375,376,377,378,379,488) and litpic = ''"; //channel='1' means the article channel can also use typeid=''
$result=mysql_query ($exec); //Specify the column ID but it can only be the ID of the smallest category, litpic = '' means the thumbnail address is empty
while($rs=mysql_fetch_object($result))
{

$id = $rs->ID;
echo $id." ";
$body = body($id); //Call function to get body content
$pic = bodyimg ($body); //Call the function to get the address of the first picture in the body
if ($pic<>1){
changearchives($pic,$id); //Call the function to insert the obtained Address
}
}
mysql_close($conn);
?>

I just learned PHP and the above code was pieced together. Beginners can also take a look at detailed comments.

Because the above code cannot automatically obtain the next-level directory, it is more manual to add the directory id. Commonly used statements are
$exec="select *. from dede_archives where typeid in(1,2,3) and litpic = ''"; //channel='1' That is, the article channel can also use typeid=''
The above code is to get the typeid as 1 and 2 and 3 data.
If you want to modify an id, you can use the following statement
$exec="select * from dede_archives where typeid='1' and litpic = ''";
If it is a channel, you can use it.
$exec="select * from dede_archives where channel='1' and litpic = ''";
Be sure to back up first, otherwise don't test it casually. Note that it is the version of dedecms5.1, other versions Should also adapt. Multiple tests (under the premise of backup.)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320736.htmlTechArticleThe specific implementation code is as follows: A brief explanation will be given below. Copy the code. The code is as follows: ?php /* 1. Configure your database connection 2. Pay attention to the prefix of the data table name. The default is dede_ 3....
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn