PHP fonksiyon verileri dönmeyecek

0 Cevap php

Ben bir PHP acemi biraz bana çok kolay gidin değilim :)

Ben kategorisinde en düşük çocuk olup olmadığını belirledikten sonra, bir arama girişi için bir kategori adını döndürmek için çalışıyorum.

Benim sql deyimi ise her kategori ve alt kategori için (birden fazla yinelenen girişleri döndürür - bir giriş Otomobil ise> Yedek Parça> Motor> Krank, Expression Engine kullanmak sizin herhangi bilmeli, 4 kez görünür yani o Bu :)) yapar

Bu sonuçlar döndürür SQL deyimi:

SELECT wd.field_id_5, wd.field_id_7, wd.field_id_14, wd.field_id_15, wd.field_id_18, wd.field_id_20, wt.entry_id, wt.title, wt.url_title, cp.cat_id, c.cat_name, c.cat_url_title, c.parent_id FROM exp_weblog_data AS wd LEFT JOIN exp_weblog_titles AS wt ON wd.entry_id = wt.entry_id LEFT JOIN exp_category_posts AS cp ON wt.entry_id = cp.entry_id LEFT JOIN exp_categories as c ON cp.cat_id = c.cat_id WHERE wt.title LIKE '%$term%'

Aşağıdaki kodu girdiyi 4 kez gösterilecek şekilde değil, derin alt kategori türetmek için çalışmaktır.

foreach ($query->result as $row)
{

  $entry_id = $row['entry_id'];
  $title = $row['title'];
  $url_title = $row['url_title'];
  $cat_id = $row['cat_id'];
  $cat_name = $row['cat_name'];
  $category_url = $row['cat_url_title'];
  $parent = $row['parent_id'];
  $image = $row['field_id_7'];
  $image_path = "example.com" . $image;
  $location = $row['field_id_14'];
  $country = $row['field_id_20'];
  $currency = $row['field_id_18'];
  $price = $row['field_id_5'];
  $postage = $row['field_id_15'];

  // if the  entry id doesnt exist in array already
  // add into array (all details)
  if ( ! array_key_exists($entry_id, $entries) )
  {
    $entries[$entry_id] = array($title, $url_title, $cat_id, $cat_name, $category_url, $parent, $image, $image_path, $location, $country, $currency, $price, $postage);
  }
}

$count = 0;

// for each entry in array, run function to find lowest child and display
function determine_child($entry_id, $cat_id, $cat_name)
{
  global $DB, $cat_name;

  $sql = "SELECT c.cat_id, c.cat_name FROM exp_categories AS c INNER JOIN exp_category_posts AS cp ON c.cat_id = cp.cat_id WHERE c.parent_id = '{$cat_id}' AND cp.entry_id = '{$entry_id}'";
  $query = $DB->query($sql);

  if ( $query->num_rows > 0 )
  {

    foreach($query->result as $cat_row)
    {
      $entry_id = $entry_id;
      $cat_id = $cat_row['cat_id'];
      $cat_name = $cat_row['cat_name'];

      determine_child($entry_id, $cat_id, $cat_name);
    }
  }
  else
  {
    return $cat_name;
  }
} // END FUNCTION


foreach ( $entries as $key => $val)
{
  $entry_id = $key;
  $cat_id = $val[2];
  $cat_name = $val[3];
  $cat_name = determine_child($entry_id, $cat_id, $cat_name);

  echo $val[0] . " - " . $cat_name . "<br />";
}

Yukarıdaki kod doğru kategoriyi dışarı echos, ama alışkanlık altındaki foreach döngüsü $ cat_name değişkene dönmek.

Yeterince açık oldum umarım, ve herhangi bir yardım için teşekkürler!

0 Cevap