Jump to content

Recommended Posts

Posted

I have the following import script being included in the homepage template file:

<?php

$mmpid = wire('pages')->get('template.name=makes')->id;

// Manufacturers:
$file = __DIR__.'/manufacturers.csv';
importCSV($file, 'mamo_manufacturer', $mmpid);

// Models:
$file = __DIR__.'/models.csv';
importCSV($file, 'mamo_model', 0, $mmpid);

function importCSV($filepath, $template, $parent_id = null, $grandparent_id = null) {
  $csv = array_map('str_getcsv', file($filepath));
  array_walk($csv, function(&$a) use ($csv) {
    $a = array_combine($csv[0], $a);
  });
  array_shift($csv); # remove column header
  //echo '<pre>'; print_r($csv); echo '</pre>';
  foreach($csv as $r) {
    $p = new Page();
    $p->name = wire('sanitizer')->pageName($r['title']);
    $p->template = $template;
    if($parent_id !== 0||null) {
      $p->parent_id = $parent_id;
    } elseif($parent_id == 0||null) {
      //$parent = wire('pages')->get('title=' . $r['parent']);
      //echo $parent.' ';//
      echo $r['parent'].' ';
      echo $grandparent_id.' ';
      $parent = wire('pages')->get('title=' . $r['parent'] . ', parent_id=' . $grandparent_id)->id;
      echo $parent.' ';
      $p->parent_id = $parent;
      unset($r['parent']);
    }
    $p->save();
    foreach($r as $k=>$v) $p->$k = $v;
    $p->save();
    echo '<br>';
  }
}

Output =

Quote

gp 0p 
gp 0p 
gp 0p 
gp 0p 
gp 0p 
gp 0p 
gp 0p 
gp 0p 
gp 0p 
Gemini Catamarans 0gp 0p 
Gemini Catamarans 0gp 0p 
Gemini Catamarans 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Lagoon 0gp 0p 
Robertson & Caine 0gp 0p 
Robertson & Caine 0gp 0p 
Robertson & Caine 0gp 0p 

Why is it running the ELSE when the condition for the IF is met? (the first 9 lines)

All 14 models (lines past 9) are created under the first manufacturer. I've been messing with it, been able to get them to display the page IDs proper at one point for the models but still there's the standing issue of all of them being created under the first manufacturer nonetheless and also the ELSE running despite not being a condition of ELSE.

What's up please...

Posted

Never mind this works the mmpid was 0 had wrong template name. 

<?php

$mmpid = wire('pages')->get('template.name=mamo_makemodel')->id;
echo $mmpid.'<br><br>';

// Manufacturers:
importCSV(__DIR__.'/manufacturers.csv', 'mamo_manufacturer', $mmpid);

// Models:
importCSV(__DIR__.'/models.csv', 'mamo_model', 0, $mmpid);

function importCSV($filepath, $template, $parent_id = null, $grandparent_id = null) {
  $csv = array_map('str_getcsv', file($filepath));
  array_walk($csv, function(&$a) use ($csv) {
    $a = array_combine($csv[0], $a);
  });
  array_shift($csv); # remove column header
  //echo '<pre>'; print_r($csv); echo '</pre>';
  foreach($csv as $r) {
    $p = new Page();
    $p->name = wire('sanitizer')->pageName($r['title']);
    $p->template = $template;
    if($parent_id !== 0||null) {
      $p->parent_id = $parent_id;
    } elseif($parent_id == 0||null) {
      //$parent = wire('pages')->get('title=' . $r['parent']);
      //echo $parent.' ';//
      echo $r['parent'].' ';
      $parent = wire('sanitizer')->pageName($r['parent']);
      $parent = str_replace('---','-',$parent);
      echo $parent.' ';
      echo $grandparent_id.'gp ';
      $parent = wire('pages')->get('title=' . $r['parent'] . ', parent_id=' . $grandparent_id)->id;
      echo $parent.'p ';
      $p->parent_id = $parent;
      unset($r['parent']);
    }
    $p->save();
    foreach($r as $k=>$v) $p->$k = $v;
    $p->save();
    echo '<br>';
  }
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...