Jump to content
CrazyEnimal

Get array of fields instead of PageArray ?

Recommended Posts

Hello!
I had a problem which is as follows:

using the $pages->find($selectors) method; I can get an array of pages in the system.
Is there any way to get an array of fields instead of an array of pages?
for example as in SQL query using not SELECT * FROM table WHERE fieldname=value; 
And I need to get an array of one field as here: SELECT some_field FROM table WHERE another_field=value;?

Let me explain:
There is an array of pages with the template "Car" where there are fields of type Manufacturer, Model, etc. which are of type Page Reference.
Suppose I found all Cars with certain parameters

$selectors = "parent=$parent,template=page_car,dealer={$dealer_id}";
$cars = $pages->findMany($selectors);

And if I need to find all the manufacturers that can be in $cars

I do it in the following way:

$manufacturerParent = wire("pages")->get($manufacturerParentID);
$manufacturerList = wire("pages")->findMany("parent={$manufacturerParent}");

foreach($manufacturerList as $manufacturerItem) {
    // I'm get all cars with selectors and with this manufacturer.
    $carsInManifacturerCount = wire("pages")->count($selectors.",manufacturer=$manufacturerItem");
    // if I have cars with this manufacturer I'm do something
    if($carsInManifacturerCount > 0 ){
        $resultManifacturerList = new StdClass();
        $resultManifacturerList->id = $manufacturerItem->id;
        $resultManifacturerList->title = $manufacturerItem->title;
        $resultManifacturerList->count = $carsInManifacturerCount;

        $marksListArray[] = $resultMarkList;
    }   
}

So here's my question, is there a more correct way to get all the Manufacturer without brute force in the same way as the SQL query given?
Thanks!

Share this post


Link to post
Share on other sites
// Source: https://processwire.com/talk/topic/3553-handling-categories-on-a-product-catalogue/?do=findComment&comment=37833

$manufacturers = new PageArray();

foreach($pages->findMany("template=carTemplate") as $car) {

	// add manufacturer from car page to manufacturers array
	$manufacturers->add($car->manufacturerField); 

};

// do whatever you want with the $manufacturers pages

Something like this?

  • Like 1

Share this post


Link to post
Share on other sites

There is another option to use Processwire explode.

As example:

$selectors = "parent=$parent,template=page_car,dealer={$dealer_id}";
$cars = $pages->findMany($selectors);
$manufacturers = $cars->explode("manufacturer");

Also later you can do and other things, $manufacturers->sort("title") etc...
And there are and other nice WireArray methods like implode, filter, and many others...
Regards.

  • Like 4

Share this post


Link to post
Share on other sites
11 hours ago, wbmnfktr said:

// Source: https://processwire.com/talk/topic/3553-handling-categories-on-a-product-catalogue/?do=findComment&comment=37833

$manufacturers = new PageArray();

foreach($pages->findMany("template=carTemplate") as $car) {

	// add manufacturer from car page to manufacturers array
	$manufacturers->add($car->manufacturerField); 

};

// do whatever you want with the $manufacturers pages

Something like this?

Sorry, but it is same brute force.

Share this post


Link to post
Share on other sites
9 hours ago, OLSA said:

There is another option to use Processwire explode.

As example:


$selectors = "parent=$parent,template=page_car,dealer={$dealer_id}";
$cars = $pages->findMany($selectors);
$manufacturers = $cars->explode("manufacturer");

Also later you can do and other things, $manufacturers->sort("title") etc...
And there are and other nice WireArray methods like implode, filter, and many others...
Regards.

Yes! explode() sounds like cool method!

But after explode() i try to do unique() of filter() and get 500 error 😞 in erro message I see "Fatal Error: Uncaught Error: Call to a member function unique() on array in"

I need to get a unique manufacturer array as well as the number of manufacturer repeats in the array.

Do you now how I can do this ?

Thanks a lot!

Share this post


Link to post
Share on other sites

What about:

// Convert a regular array into a WireArray again

$wireManufacturers = new WireData();
$wireManufacturers->setArray($manufacturers);

Idea comes from here.

Share this post


Link to post
Share on other sites

@CrazyEnimal, please insert your code inside a code block in forum posts.

16 hours ago, CrazyEnimal said:

for example as in SQL query using not SELECT * FROM table WHERE fieldname=value;

You can use SQL in ProcessWire when it suits you. That's what the $database API variable is for.

Here is one way you could get a listing of manufacturers with the number of occurrences within a selection of cars.

// Get the IDs of the cars
$car_ids = $pages->findIDs("parent=$parent, template=page_car, dealer=$dealer_id");
// Get table for manufacturer Page Reference field
$table = $fields->get('manufacturer')->getTable();
// Get manufacturers that are selected in the car pages
$query = $database->query("SELECT data FROM $table WHERE pages_id IN (" . implode(',', $car_ids) . ")");
$manufacturer_ids = $query->fetchAll(\PDO::FETCH_COLUMN);
// Count how many times each manufacturer occurs in the results
$manufacturer_occurrences = array_count_values($manufacturer_ids);
// Sort the results in order of occurrences, highest to lowest
arsort($manufacturer_occurrences);
// Get the manufacturer pages
$manufacturers = $pages->getById(array_keys($manufacturer_occurrences));
// Output a list of manufacturer titles and the number of occurrences
foreach($manufacturers as $manufacturer) {
	echo "<p>{$manufacturer->title} ({$manufacturer_occurrences[$manufacturers->id]})</p>";
}

 

  • Like 5

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...