Dedecms batch extraction of the first picture the most thumbnail code of article + software
- 2020-03-31 16:47:16
- OfStack
The specific implementation code, as follows: the following will give a simple description.
I've just learned PHP and I've put the code together and I've done a lot of detailed comments that beginners can look at
Because the above code cannot automatically get the next level of directory, so manually add directory id, common statements are.
$exec="select * from dede_archives where typeid in(1,2,3) and litpic =" "; //channel='1' means the article channel can also use typeid=''
The code above gets the data for typeids 1 and 2 and 3.
If you change an id, use the following statement
$exec="select * from dede_archives where typeid='1' and litpic =' '";
You can use it if it's a channel
$exec="select * from dede_archives where channel='1' And litpic = "";
Make sure you back up first, or don't just test it, it's the dedecms5.1 version, other versions should work as well. Multiple tests (on the premise of backup).
<?php
//Database connection Settings
$ip = "localhost"; //address
$user = "jb51"; //The user name
$pw = "www.jb51.net"; //password
$db = "jb51"; //The name of the table
$conn=mysql_connect ($ip,$user,$pw);
mysql_select_db($db);
mysql_query("set names gbk"); //Set the code to GBK
//To 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 ;
}
//The end! Get body data
// start ! Extract the first image in the variable address
function bodyimg($obj) {
if(isset($obj)){
if ( preg_match( "<img.*src=["](.*?)["].*?>", $obj, $regs ) ) { // Get the first image using regularization address
return $obj = $regs[1] ;
} else {
echo "Erro: No picture address! <br>";
return $obj = 1 ;
}
}
}
// The end of the ! Extract the first image in the variable address
// start Modify the rchives, 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 " Modified data successfully! <br>";
else echo " Data modification failed !<br>";
}
// The end of the Modify the rchives, Insert the obtained image address
//Displays the contents of the data table
$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 column ID but only the minimum category ID, litpic = "means the thumbnail address is empty
while($rs=mysql_fetch_object($result))
{
$id = $rs->ID;
echo $id." ";
$body = body($id); //Call the function to get the body content
$pic = bodyimg($body); // Call function fetch body The first picture inside address
if ($pic<>1){
changearchives($pic,$id); // The calling function inserts the obtained address
}
}
mysql_close($conn);
?>
I've just learned PHP and I've put the code together and I've done a lot of detailed comments that beginners can look at
Because the above code cannot automatically get the next level of directory, so manually add directory id, common statements are.
$exec="select * from dede_archives where typeid in(1,2,3) and litpic =" "; //channel='1' means the article channel can also use typeid=''
The code above gets the data for typeids 1 and 2 and 3.
If you change an id, use the following statement
$exec="select * from dede_archives where typeid='1' and litpic =' '";
You can use it if it's a channel
$exec="select * from dede_archives where channel='1' And litpic = "";
Make sure you back up first, or don't just test it, it's the dedecms5.1 version, other versions should work as well. Multiple tests (on the premise of backup).