Check one column cell to another entire column in PHP - php

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

Related

How to find correct columns by data from 1st row phpexcel

I have excel file with some certain data i need.
"Category" is in first row of excel file.
Thing is that this data can be in different columns.
Is it possible to get column name based on data in first row ?
Example sheet:
For example, i need to get aircraft type and category, wich in this case are columns I & H.
At the moment i do it like this:
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
for($i=2;$i<=$arrayCount;$i++){
$aircraftType = trim($allDataInSheet[$i]["I"]);
$flightCategory = trim($allDataInSheet[$i]["H"]);
#insert all data in these columns to database
$query = "INSERT INTO {$databasetable} (aircraftType, flightCategory)
VALUES('$aircarftType', '$flightCategory')";
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
}
But how can i do it, when i dont know column letter?
Maybe in next file aircraftType is in K and flightCategory is in L...
So is there a way to get column letter based on data in first row.
I think i could propably do it if i save all data to database, and then take it out with some where clauses, but maybe there is better way of doing this directly with PHPExcel ?
I am not really good explaining things, so sorry if this is confusing, please ask and i try to explain more.
Thanks in advance for help.
A simple call using the worksheet's rangeToArray() will give you an array of the headers
$headers = $objPHPExcel->getActiveSheet()
->rangeToArray(
'A1:'.$objPHPExcel->getActiveSheet()->getHighestColumn().'1',
null,
false,
false,
true
);
$headers = $headers[1];
The resultant array has the column letter as the key, and the cell content as the value; and you can then use it to map the appropriate columns to your data

finding "orphan" files in website structure

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
}
}

php sql:can contents of a word document being split and then insert to database in single row each

sorry for disturbing. I have a table on database call KEYWORDS..contains KEY column..I have a bunch of keywords list in 1 file of docx format called keywords.docx.. each keywords in the file is separated by comma..the keywords will be like this:
environment, country pollution, animal habitat problem, water, energy..
So up there, they are 5 keywords. The idea is I will upload my keywords.docx to the page and then click save button.. in the database server side, the keywords will be automatically insert to the KEY column separately. That means they will have 5 rows of keywords in KEY column under KEYWORDS table. Is it possible in php and mysql..???
Thank you very much for your help and opinion... :)
You can use explode method in PHP somthing like this:-
<?php $data="ONE TWO THREE FOUR FIVE";
$splittedstring=explode(" ",$data,-2);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>"; }
?>
its output will be like this:-
splittedstring[0] = ONE
splittedstring[1] = TWO
splittedstring[2] = THREE
See more at:
http://braincybersolutions.com/php-tutorial/php-explode-split-a-string-by-string-into-array/#sthash.tnUaGi9w.dpuf
After this you can store your values in your database.

MySQL Database & PHP - Match row with files and append extension

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 ;)

PHP - Echo amount of pictures based on mysql_num_rows

I want to code a script that will echo an amount of images based on the mysql_num_rows(); function.
Like, I'm storing 3 in a row. Then I'll create:
$images = mysql_num_rows($count);
Then I would like it to echo 3 images.
Is this possible?
If you don't get the question or want me to rewrite it, please just tell me.
Then i think, all you need to do is fetch the rating (i.e. a number) from the database and just use that number directly to display the number of stars. Coz mysql_rows_count would give you the total number of records in a result set which is returned as a result of a select query on multiple columns, but in your case you only need to access one column which would contain the rating and another column which would probably contain user id or some sort of key to associate the ratings with. The result set can be fetched as an associative array which can then be used by indices to fetch the right column. Have a look at this: PHP-mysql_fetch_array() .
EDIT: just to sum up (not the actual syntax) :
$array = mysql_fetch_array(<your select query fetching only the needed columns>);
$images = $array[0] // assuming the rating number is at the 0th index
//image display code, do your stuff
So here is what I did:
I had the numbers from 1-5 in my database in a row called 'rating'. Now when I picked this out of the database I stuffed the number (1-5) into a variable called '$starRating'.
for($i=1;$i<=$starRating;$i++){
echo "<span class='star'><span class='starText'>$i</span></span>";
}
Then I created a class called star and starText:
.star{
width:18px;
height:18px;
background:url('../images/design/smallImg/star.png') no-repeat;
text-align:center;
float:right;
margin-top:2px;
}
/* star content */
.starText{
display:none;
}
.starText is made to hide the text that would've been from the for() loop.
* I don't know if you can avoid the text another way around *
This works for me perfectly.
I wrote this to help people searching for an easy solution to this.

Categories