-
- 24,544 Posts
Sorry I didn't see this thread earlier. I would think you'd want the list as a CSV file and just run a simple utility snippet to import the new values. I could give you an example if you'd care to post a sample showing the format of the data in the list.
-
- 112 Posts
Thank you for your reply Bob! I solved it by importing directly through phpmyadmin, but it would be interesting to see your example if you don't mind?
The data was formated in a csv like this: id;tv1;tv2;tv3
-
- 24,544 Posts
It would look something like this (untested):
<?php
$fp = fopen('csvFile', 'r');
$i = 0;
$output = '';
while (!feof($fp)) {
$currentLine = fgets($fp);
$values = explode(';', $currentLine);
if (count($values) != 4) {
$output .= '<br/>Badly formatted line: ' . implode(';', $values);
/* don't use line */
continue;
}
if (!empty($currentLine)) {
$i++;
$resourceId = (integer)$values[0];
unset($values[0]);
/* renumber array indexes */
$values = array_values($values);
/* get the resource */
$resourceObj = $modx->getObject('modResource', $resourceId);
/* set TV names */
$tvs = array(
'tv1Name',
'tv2Name',
'tv3Name'
);
/* loop to set TV values */
if ($resourceObj) {
for ($i = 0; $i < 2; $i++) {
$resourceObj->setTVValue($tvs[$i], trim($values[$i]));
}
}
}
}
fclose($fp);
$output .= '<br/>Processed ' . $i . ' lines';
return $output;
[ed. note: BobRay last edited this post 9 years, 9 months ago.]
-
- 112 Posts
Thank you for that, Bob! Will definitely try that one next time it needs updating.
-
- 24,544 Posts
You'll have to correct the TV names in the code -- don't forget that they're case-sensitive (and the name of the file).
I've added a small test for the parsed lines' integrity, empty lines, and a final report to the code above.
[ed. note: BobRay last edited this post 11 years, 3 months ago.]
-
- 24,544 Posts
It's a mistake in the code - I've corrected it above.
$resource->setTVValue($tvs[$i], trim($values[$i]));
should be:
$resourceObj->setTVValue($tvs[$i], trim($values[$i]));
IOW, $resource-> should be $resourceObj->
-
- 112 Posts
Thank you Bob, that did the trick.
Now it's importing as it should - only thing i'm wondering is, that the last of the three tv's doesn't get imported. Only the first two? But I can work around that by changing the file...
-
- 24,544 Posts
Change this:
for ($i = 0; $i < 2; $i++) {
to this:
for ($i = 0; $i <= 2; $i++) {
That will give you three.