I've got a folder full of files pulled into an array:
Array
(
[2] => 2_1_page2-img5.jpg
[3] => 2_2_page2-img5-big.jpg
[4] => 3_1_page2-img6.jpg
[5] => 3_2_page2-img6-big.jpg
[6] => 4_1_page2-img3.jpg
[7] => 4_2_page2-img3-big.jpg
[8] => 6_2_page2-img3-big.jpg
[9] => 8_2_page2-img3-big.jpg
)
I'm trying to create a script that will rename them all so they start at 1 and rename incrementally. The ideal solution would be:
Array
(
[2] => 1_1_page2-img5.jpg
[3] => 1_2_page2-img5-big.jpg
[4] => 2_1_page2-img6.jpg
[5] => 2_2_page2-img6-big.jpg
[6] => 3_1_page2-img3.jpg
[7] => 3_2_page2-img3-big.jpg
[8] => 4_2_page2-img3-big.jpg
[9] => 5_2_page2-img3-big.jpg
)
I won't know whether each number will have one or two images attached to it.
So far I've got:
$filecounter = 1;
$previousNumber = $filecounter;
foreach($listing as $imagename){
// $imagename: the original filename
// $prefixNumber: the first number from the filename
// $reducedfilename: the filename without the prefix
// $previousNumber: the previous number
// $filecounter: the count up from 1
$prefixNumber = stristr($imagename, '_',true);
$reducedfilename = ltrim(stristr($imagename, '_'), '_');
if ($prefixNumber != $filecounter && $prefixNumber != $previousNumber){
$previousNumber = $filecounter;
$filecounter++;
echo "filecounter: $filecounter<br>prefixNumber: $prefixNumber<br>previousNumber: $previousNumber<br>";
echo "1. Renaming: " .$imagename . " to " . $filecounter . "_" . $reducedfilename . "<br><br>";
//rename($directory . $imagename, $directory . $filecounter. "_" . $reducedfilename);
} else {
echo "filecounter: $filecounter<br>prefixNumber: $prefixNumber<br>previousNumber: $previousNumber<br>";
echo "2. Renaming: " .$imagename . " to " . $previousNumber . "_" . $reducedfilename . "<br><br>";
//rename($directory . $imagename, $directory . $previousNumber . "_" . $reducedfilename);
}
I've given myself a ton of visual pointers to try and help but I think I've been staring at it too long and I honestly can't work it out.
Does anyone please have any ideas what I'm doing wrong here?
I think something like this should probably work for you.
$filecounter = 1;
$currentPrefix = false;
$lastPrefix = false;
foreach ($listing as $imagename) {
$aFileParts = explode('_', $imagename);
$currentPrefix = $aFileParts[0];
if (!$lastPrefix) {
$lastPrefix = $currentPrefix;
}
if ($currentPrefix != $lastPrefix) {
$filecounter++;
$lastPrefix = $currentPrefix;
}
$newFileName = $filecounter . '_' . $aFileParts[1] . '_' . $aFileParts[2];
}
Related
I am trying to pull url string items (utm fields), combine them into one variable (vtid) and pass to page but it is only passing dashes, the url info is not passing.
<?
$utm_source = $_REQUEST['utm_source'];
$utm_medium = $_REQUEST['utm_medium'];
$utm_campaign = $_REQUEST['utm_campaign'];
$utm_term = $_REQUEST['utm_term'];
$utm_content = $_REQUEST['utm_content'];
$vtid = $utm_source . "-" . $utm_medium . "-" . $utm_campaign . "-" . $utm_term . "-" . $utm_content;
?>
print_r($_REQUEST); shows
Array (
[gr_subscriber] => exists
[email] => myemail#me.com
[http_referer] => example.com=&utm_source=as&utm_medium=ms&utm_campaiā¦
[webforms_id] => 657433
[name] => Friend
)
Whenever I run this code through the SalesForce PHP api, it fails with err:Notice: Trying to get property of non-object
$query ="SELECT accountId,Status,Id,Service_Account_DMKT__r.name,(select Activity_Type__c from Tasks) from case where Owner.Name ='" . $name . "' AND CreatedDate = THIS_MONTH AND Record_type_name__c='Performance Reviews' AND status NOT IN ('')";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
foreach($queryResult->records as $case){
//for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
$callCounter = 0;
$emailCounter = 0;
$accountId = $case->current()->accountId;
$accountName=$case->current()->Service_Account_DMKT__r->Name;
$caseId= $case->current()->Id;
if($case->any['Tasks']->records) {
$counter=0;
foreach($case->any['Tasks']->records as $record) {
$taskRecord = $record->any;
if (strpos($taskRecord, 'Call - Outbound') !== false) {
$callCounter++;
} else {
$emailCounter++;
}
$counter++;
}
}
echo '<p>AccountName=' . $accountName . '</p><p>CaseId=' . $caseId . '</p>';
echo '<p>' . $callCounter . ' Calls and ' . $emailCounter . ' emails';
echo'<hr>';
$index++;
}
print_r($case);
I know it is because of these three lines. I'm not stepping through the object correctly.
$accountId = $case->current()->accountId;
$accountName=$case->current()->Service_Account_DMKT__r->Name;
$caseId= $case->current()->Id;
But I'm not sure what to use instead of current(). Below is the response object from the SF API
stdClass Object
(
[type] => Case
[Id] => Array
(
[0] => 5000e00001J7L0pAAF
[1] => 5000e00001J7L0pAAF
)
[any] => Array
(
[0] => 00130000002bqXiAAIClosed - Contact Declined5000e00001J7L0pAAF
[Service_Account_DMKT__r] => stdClass Object
(
[type] => Account
[Id] =>
[any] => brinsoncorsicanafordfd
)
[Tasks] => stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => Task
[Id] =>
[any] =>
)
)
[size] => 1
)
)
)
I finally managed to fix it by converting the response back to another object
$query ="SELECT accountid,Status,Id,Service_Account_DMKT__r.name,(select Activity_Type__c,subject from Tasks) from case where Owner.Name ='" . $SFName . "' AND CreatedDate = THIS_MONTH AND Record_type_name__c='Performance Reviews' AND status NOT IN ('')";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
foreach($queryResult->records as $case){ //For every record within $queryResult
$callCounter = 0; //Set up our task counters
$emailCounter = 0;
$sObject = new SObject($case); //turn $case back into a SObj to easy step thru
$accountId= $sObject->AccountId; //Pull AccountId from $sObject
$accountName=$sObject->Service_Account_DMKT__r->Name;
$caseId=$sObject->Id;
$caseStatus=$sObject->Status;
if(!isset($sObject->queryResult)) { //Check if there are any tasks on the record, otherwise we'll get an error
$callCounter=0; //if there are no tasks, set counters to 0
$emailCounter=0;
}else{
$counter=0;
foreach($case->any['Tasks']->records as $record) { //for each task in the $case
$taskObject = new SObject($record); //Turn $record into taskObject so we can step through it.
$taskType = $taskObject->Activity_Type__c; //Pull the activity type out of TaskObject
if($taskType == "Call - Outbound"){ //Calling $taskType actually allows us to compare the obj to a string, where as going through this in an array format would not!
$callCounter++; //increase counter if the taskType is a call
} else {
$emailCounter++;
}
}
}
echo '<p>AccountName=' . $accountName . '</p><p>AccountID=' . $accountId . '</p><p>CaseId=' . $caseId . '</p><p>CaseStatus=' . $caseStatus . '</p>';
echo '<p>' . $callCounter . ' Calls and ' . $emailCounter . ' emails';
echo'<hr>';
}
Looking for a little guidance as I don't know how to echo all my set options into an asana request. So I have an array from a user submitted form:
Array
(
[_wpcf7] => 5
[_wpcf7_version] => 4.1.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f5-o1
[_wpnonce] => 1531937d4e
[your-name] => TEST
[your-email] => test#test.com
[title] => test
[Description] => test
[venues] => Dragonfly
[duedate] => 2015-04-11
[options] => Array
(
[0] => Print Business Card (3.5x2)
[1] => Google Ads Package
[2] => Print Ticket (1.5x5.5)
[3] => Print Magazine Ad (8x10.75)
)
[_wpcf7_is_ajax_call] => 1
[upload] => ditch_fridays_logo.png
)
From there I parse the array to grab the variables:
$submittedBy = $posted_data['your-name'];
$eMail = $posted_data['your-email'];
$projectTitle = $posted_data['title'];
$projectDescription = $posted_data['Description'];
$venue = $posted_data['venues'];
$deadline = $posted_data['duedate'];
$options = $posted_data['options'];
$upload = $posted_data['upload'];
And then I create the asana task
// First we create the task
$result = $asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => $projectTitle, // Name of task
'assignee' => 'somewhere#somewhere.com', // Assign task to...
'due_on' => $deadline,
'notes' => 'Submitted By: ' . $submittedBy . "\n" . 'E-Mail: ' . $eMail . "\n\n" . '--------------------------------------------------' . "\n\n" . 'Task Name: ' . $projectTitle . "\n\n" . 'Description: ' . $projectDescription . "\n\n" . 'Venue: ' . $venue . "\n\n" . 'Deadline: ' . $deadline . "\n\n" . 'Attachments: http://inkentertainment.com/graphics/saved/' . $upload
));
The problem is, I cant include the selected options in the notes part of the task creation. How would I go about including all the options ($posted_data['options']) only if the user has selected them? (on the form there is a list of about 10, but the users will select 2 or 3)
I hope I have been clear enough for you to understand, if not let me know and I will try and clarify
$options = ( isset ( $posted_data[ 'options' ] ) &&
is_array( $posted_data[ 'options' ] ) ) ?
$posted_data[ 'options' ] : array();
here you'll have an array with zero or more elements.
If you need a string you may then use implode
http://php.net/manual/en/function.implode.php
ex: $options = implode( ', ', $options );
After hours going around in circles I gave up and ask for help.
I have an array which looks like:
[field01] => Array
(
[name] => test01
[prefix] => C01
)
[field02] => Array
(
[url] => http://www.url.com
[user] => a_user
[password] => a_password
)
[filed03] => Array
(
[0] => Array
(
[id] => 1
[Type] => standard
[Name] => name
)
[1] => Array
(
[id] => 5
[Type] => standard
[Name] => name
)
)
Now I want to go through that array and get the following output as a return from a recursive function:
Array (
[0] = "The values of field01: name, prefix - test01, C01"
[1] = "The values of field02: url, user, password - http://www.url.com, a_user, a_password"
[2] = "The values of field03: id, Type, Name - 1, standard, name"
[3] = "The values of field03: id, Type, Name - 5, standard, name"
)
I have tried it with a recursive function but stuck with field03 to save the name.
Any help for that?
UPDTAE:
here is the array, so you don't have to write it:
$data['field01']['name'] = "field01";
$data['field01']['prefix'] = "C01";
$data['field02']['url'] = "http://www.url.com";
$data['field02']['user'] = "a_user";
$data['field02']['password'] = "a_password";
$data['field03'][0]['List_id'] = 1;
$data['field03'][0]['Type'] = "standard";
$data['field03'][0]['Name'] = "name";
$data['field03'][1]['List_id'] = 5;
$data['field03'][1]['Type'] = "standard";
$data['field03'][1]['Name'] = "name";
Thanks in advance!
<?php
$data['field01']['name'] = "field01";
$data['field01']['prefix'] = "C01";
$data['field02']['url'] = "http://www.url.com";
$data['field02']['user'] = "a_user";
$data['field02']['password'] = "a_password";
$data['field03'][0]['List_id'] = 1;
$data['field03'][0]['Type'] = "standard";
$data['field03'][0]['Name'] = "name";
$data['field03'][1]['List_id'] = 5;
$data['field03'][1]['Type'] = "standard";
$data['field03'][1]['Name'] = "name";
$text = '';
foreach ($data as $k => $fields) {
if (isset($fields[0])) {
foreach ($fields as $field) {
$text .= 'The values of ' . $k . ': ' . traverse($field) . "\n";
}
} else {
$text .= 'The values of ' . $k . ': ' . traverse($fields) . "\n";
}
}
echo $text;
function traverse($fields) {
$keys = array_keys($fields);
$values = array_values($fields);
return implode(', ', $keys) . ' - ' . implode(', ', $values);
}
?>
This assumes if there is an array of arrays, it is all arrays. In other words, you aren't mixing strings with arrays.
$resultArray = array();
function process($inputArray, $fieldName) {
$result = array();
$keys = array_keys($inputArray);
if (!is_array($inputArray[0])) {
$result[] = "The value of $fieldName: ".implode($keys,', '). ' - '. implode($inputArray, ', ');
} else {
foreach($inputArray as $arr) {
$result = array_merge($result, process($arr, $fieldName));
}
}
return $result;
}
foreach($data as $k=>$v) {
$processed = process($v, $k);
$resultArray = array_merge($resultArray, $processed);
}
print_r($resultArray);
Ok, I have 2 arrays. 1 array has the ORDER of the information that should be displayed, and the other array has the information. But I am having trouble trying to determine a way to display the information in the order it is supposed to be displayed in...
For example, I have this array here, OF WHICH I HAVE NO CONTROL OVER (meaning the array gets done prior to my code even running, so I have to START with this array of which I can NOT alter this actual array. I could build an array from this array if it would help any? But not sure how...?)
$attach_info(0,1,5,4);
The numerical values here point to actual information to be displayed...
0 = Members name ($attachments['member']['link'])
1 = Filename ($attachments['file']['link'])
2 = Filesize ($attachments['file']['filesize'])
3 = Downloads ($attachments['file']['downloads'])
4 = Link to post ($attachments['topic']['link'])
5 = Time of attachment ($attachments['topic']['time'])
6 = Image to display ($attachments['file']['image']['link'])
The other array gets populated within a function and get RETURNED from that function with all of the information to be displayed...
The returned information is this array:
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$attachments[$row['id_attach']] = array(
'member' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '' . $row['poster_name'] . '',
),
'file' => array(
'filename' => $filename,
'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
'downloads' => $row['downloads'],
'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
'link' => '<img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /> ' . $filename . '',
'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
'image' = array(
'id' => $id_thumb,
'width' => $row['width'],
'height' => $row['height'],
'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
'link' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
),
),
'topic' => array(
'id' => $row['id_topic'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
'link' => '' . $row['subject'] . '',
'time' => timeformat($row['poster_time']),
),
)
}
AND this is just an example really, cause this array gets built within that function for EACH attachment, which goes into another array. Ofcourse there is MORE info. here than I need, but I will shrink it down later.
So, currently I am doing a foreach on the $attachments array to grab all of the attachments, and this gives me all of the information for the attachments as well. No problem with any of that.
The problem I am having is how to grab the information from the $attachments array in the order from the $attach_info array. I need the output to be what is in the attach_info array number-wise, in the order it is in.
For example, if $attach_info array is like this:
$attach_info(0,1,5,4);
Than it should output the Members name first, than the Filename, than the Time of the Attachment, and lastly a link to the post.
So the order of this array is important. And should grab this information from the $attachments array.
Can someone please help me with a way to set this up within a foreach loop so that the information gets echo'd in the correct order. And the attach_info array can change ofcourse. I'm looking for a fast approach to doing this. I'd rather not have to determine the order of the information and what information should be displayed everytime for each loop within the $attachments array if you know what I mean.
Thanks you guys Rock :)
EDIT
The $attachments array listed above is only 1 output of the actual array. It is actually outputting ALL attachments, so I need it to be able to LOOP through all attachments and still output the correct order and the correct information ONLY! Sorry if I failed to explain this.
$attachments array is 1 loop within an foreach... so it is just the output of only 1 foreach. I need them all.
So, what I can do now is this...
foreach($attachments as $item)
{
echo
$item['member']['link'] . $item['file']['link'] . $item['file']['filesize'] . $item['file']['downloads'] . $item['topic']['link'] . $item['topic']['time'] . $item['file']['image']['link'];
}
BUT I need to echo the items in the correct order according to $attach_info array, and only echo those items!
best of all try this:
foreach($attach_info as $val){
switch($val){
case 0: echo $attachments['member']['link'];
break;
case 1: echo $attachments['file']['link'];
break;
case 2: echo $attachments['file']['filesize'];
break;
case 3: echo $attachments['file']['downloads'];
break;
case 4: echo $attachments['topic']['link'];
break;
case 5: echo $attachments['topic']['time'];
break;
case 6: echo $attachments['file']['image']['link'];
break;
}
}
May be it helps http://php.net/manual/ru/function.usort.php