i've got the following problem:
I've taken over a MS-SQL Database from my superior which has been developed by him. Sadly the database is in really bad shape for development.
The Database has already been "converted" to MySQL by me and the data imported. Now the problem is, theres a table "hotels" which had got rows named "image1, image2, image3" etc up to image24. I removed them from the table and created a new table called hotel_images where the images are assigned to a hotel. Now to describe my problem :
The imported data contained strings for each image such as "007593-20110809-145433-01" but the extension was missing. All the images were placed in the same directory (there are about 4000) and only the string has been saved.
I already did a workaround function myself when pulling the data into the website where i check file_exists and then return the different extensions (.BMP, .GIF, JPG etc) but i don't like this solution.
Is there any possiblity for me to check all strings available in a single table with the image folder and add the proper extension to the table if the string matches? It must be something like
SELECT image from hotel_images (search for value in /images/) IF MATCH ALTER TABLE hotel_images set image = this + .extension
I would appreciate any advice!
Edit: it just came to my attention that i could do a dir listing to a text-file from the folder and then match it against every string in the table and if match replace it - is that a possible solution?
You have to SELECT all rows (without extension)
Then, in PHP, foreach on all images to find if they're existing in the folder, take their extensions
Then UPDATE row with the existing filename, adding the extension...
With an example :
$images_query = mysql_query("SELECT id, image_name from hotel_images");
while($image = mysql_fetch_array($images_query)){
if(file_exists($image["image_name"])){
//Get extension
$ext = "...";
//Then update row with new name
mysql_query("UPDATE hotel_images SET image_name = '" . $image["image_name"] . $ext ."' WHERE id = " . $image["id"]);
}
}
Are you searching for something like that ?
It's not tested script, did it directly in the SO textarea ;)
Related
I have a csv file which contain two columns
first column with Part number and second one is Image Name (Multiple Image for each product and Image Name based on Part Number )
I want to check each Part number with entire second column of Image Name each time as this is not sorted properly and store that image name for that perticuler part number in another csv along with that part number.
Current CSV
PartNumber Image Name
WP35001153 35001153R.jpg
WPW10135901 35001153R_Back.jpg
WPW10184873 W10135901R.jpg
WPW10200900 W10135901R_BACK.jpg
WPW10215493 W10137702R.jpg
WPW10249237 W10137702R_Back.jpg
WPW10258402 W10141364R.jpg
WPW10477076 W10477076R.jpg
WP8194064 W10477076R_Back.jpg
W10479760R.jpg
As above csv Part one WP35001153 have two images in on right side 35001153R.jpg and 35001153R_Back.jpg
Also Part number column and Image Column is not perticulary or properly assigned.
So I want to search Part Number with Image name entrie column and if it matches then store in Image1 Column and more than two times it should be store in next column Image2 like below.
PartNumber Image1 Image2
WP35001153 35001153R.jpg 35001153R_Back.jpg
WPW10135901 W10135901R.jpg W10135901R_BACK.jpg
WPW10200900 W10200900.jpg
Please help to figure it out
Thanks
Seem you need to build a new array, using your first column (of csv), cleaned as a key and add to it all you find related too.
Exemple (not exact code)
$mynewarray = [];
foreach ($mycsv as $line) {
$id = $line['PartNumber'];
if ($id[2] == 'W') {
it's a key, clean to obtain : 35001153
mynewarray[$id] = ['pics'=>[ ... ]];
} else {
it's a pic, add the image name
clean the id image name to obtain the key 35001153
mynewarray[$id]['pics'][] = the pics;
}
}
If i understand well your need, this is the idea can solve your problem in one loop with few id analyze.
:) enjoy
I have a site that I've built that is connected to a mysql database. I have items (listed in the database) that correspond to pictures. Database structure is:
Item_num Description Price Available
Item_num is a unique alphanumeric (A001).
In my images folder, I have several photos of A001, labeled:
A001_full.jpg
A001_thumb.jpg
A001_model.jpg
This is fairly consistent. Some pictures don't have the _model version, but all have the _thumb and _full versions. Unfortunately, I added a bunch of pictures then abandoned the site. As i'm bringing it back online, a lot of those pictures do not have an SQL entry to match them. What I would like to do is this:
Import the directory listing of the images (../images)
grab the first part of the file name (before the '_'), thinking strtok for that
use that token to query against the Item_num key in the database
if that key is found, move to the next file
if that key is not found, output the token to the page
i'm really unsure of the directory listing and how to handle that, and how to run multiple repeated queries to the database. I'm used to running one query then using the results from that. Any help on this would be highly appreciated
You can actually do this with a single query, and then loop through the results. It will be faster than running one query per entry. Something like this:
$query = "SELECT Item_num FROM table";
Then we'll assume you've dumped the result an indexed array where each element is the value of Item_num for each row into $result (the specifics of creating this array may depend on how you're interacting with the database):
// Loop through all images
foreach ( glob( '../images/*' ) as $image ) {
// Get the portion of the name before the underscore
list( $image_item ) = explode( '_', $image );
// Compare this to all of the return values
if ( !in_array( $image_item, $result )) {
// The image is not in the database
}
}
I have a mySQL db table. One of the table columns contains URLs which point to different xml files on a remote server.
My goal is to read each URLs info and write the xml content into another column on the same record (line) respectively.
In my PHP code, I am able to get the URL correctly from mySQL database and I am able to get the XML content on remote server into a variable correctly.
But the issue is while I write the content to my table line by line. Some XML columns got update correctly and some XML columns are empty.
I am pretty sure each time the variable got content correctly because I am able to print out each individual content on screen.
Why are some content updating the column and some don't. All the XML strings have the same format. If I copied that content and updated the mysql table manually, it successfully wrote into the table.
At beginning I thought it was time issue so I add enough sleep time for my PHP code. it does't help. then I suspected my db datatype, so I changed the XML
column data type from VCHAR to TEXT and even LONGTEXT. it does't help either. Does any one have a clue?
part of my php code below...
$result = mysqli_query($con,"SELECT url_txt FROM mytable ");
//work with result line by line:
while($row = mysqli_fetch_array($result)) {
echo $url_content = file_get_contents($row['url_txt']);
//debug line below *******************************/
echo $URL=$row[url_txt];
//debug line above********************************/
mysqli_query($con,"UPDATE mytable SET xml_info='$url_content' where url_txt = '$URL' ");
}
Maybe try converting your XML to a native array and working with it that way:
$array = json_decode(json_encode(simplexml_load_string($url_content)),TRUE);
I'm pretty new to web development so there's a good chance I'm doing something pretty dumb here.
I'm using AJAX to send data to a PHP file which will use the data to run SQL commands to update a table. I'm dealing with editing articles, so my PHP file needs to know three things: The original name of the article (for reference), the new name and the new content. I also tell it what page the user is looking at so it knows which table to edit.
$('#save_articles').click(function () {
var current_page = $('#current_location').html();
var array_details = {};
array_details['__current_page__'] = current_page;
$('#article_items .article_title').each(function(){
var article_name = $(this).html(); //The text in this div is the element name
var new_article_name = $(this).next('.article_content');
new_article_name = $(new_article_name).children('.article_content_title').html();
var new_article_content = $(this).next('.article_content');
new_article_content = $(new_article_content).children('.article_content_content').html();
array_new_deets = {new_name:new_article_name, content:new_article_content};
array_details[article_name] = array_new_deets;
});
send_ajax("includes/admin/admin_save_articles.php", array_details);
});
In the PHP file, I first retrieve the current page and store it in $sql_table and then remove the current page variable from $_POST. Then I run this.
foreach($_POST as $key => $value){
$original_name = $key;
$new_name = $value['new_name'];
$new_cont = $value['content'];
$query = "UPDATE
`$sql_table`
SET
`element_name`= '$new_name',
`element_content` = '$new_cont',
WHERE
`element_name` = '$original_name'";
$query = mysql_query($query);
if(!$query){
die(mysql_error());
}
}
I always receive an error saying that 'sitep_Home' is an incorrect table name. Not only is it a real table in my db, but I've actually changed its name to make sure it isn't an issue with keywords or something.
If I instead run the query without the variable $sql_table (specifying that the table is called 'sitep_Home'), the query accepts the table. It then doesn't actually update the table, and I suspect it's because of the WHERE argument that also uses a variable.
Can anyone see what I'm doing wrong here?
try to use $sql_table as '$sql_table' if you are sure that this contain a right table name.
Like you are using other column's value
Check if this can help!!
Dump/log your query before executing it - the problem should be quite visible after that (I suspect some additional characters in the table name).
Couple of things:
you should never trust your users and accept everything they'll send you in $_POST, use whitelist for the fields you'd like to update instead
your code is vulnerable to SQL injection, I recommend to use some framework / standalone library or PDO at least, avoid mysql_query which will be deprecated in the future. Check this to get some explanation http://www.phptherightway.com/#databases
Table names are case sensitive in MySQL. Please check if there is mistake in the case.
You have to surround name of mysql table in query in this `` qoutes. When you dinamically create mysql table it is very important to trim($variable of mysql name table) before create, because if "$variable of mysql name table" have space in the edns or in the start mysql not create table. And the last when you call dinamically $variable of mysql name table in query you have to trim($variable of mysql name table) again.
I have a mySQL database with the following
e.g.
Microeconomics.
The ‘theory of the ï¬rm’
or:
Resource allocation modiï¬cations.
For some reason, the text that has been input (through CKEditor), has been changed so any instances of fi are in the database as 'ï¬'. I believe this is something to do with HTML entities. The text (I believe) was copy-pasted from a word document, which could be part of the problem.
How do I change (in PHP or mySQL) all instances of 'ï¬' into fi? When rendered as a PDF by TCPDF, it shows a ? (e.g. financial = ?nancial, significant = signi?cant)
Thanks in advance.
This statement will correct all the false instances for the column that contains the 'ï¬'
Update table Set
column = replace(column, 'ï¬', 'fi')