Jump to content

PHP method help/adivce


mahalleday

Recommended Posts

I have written a method that gathers data from several tables in my database and builds a ad out of the information then returns multidimensional array for each ad like this:

 

$ad[0]
$ad['title'][0]
$ad['price'][0]
$ad['description'][0]...etc

 

The method i have looks something like this:

 

function makeAd($id=null){
//if ad id is given then only get that ad
  if($id != NULL) {
    $where = "WHERE ad_id = $id";
  }
  else {$where = "";}

$sql = "SELECT * FROM ads $where";
$query = mysql_query($sql);

//use while loop to make ad
  while($ret_val = mysql_fetch_array($query, MYSQL_ASSOC)):
//get ads category, subcategory and images etc from other tables
  $cat_query = mysql_query("SELECT cat_name FROM categories WHERE $category_id = ".$ret_val['category_id']);
  $value = mysql_fetch_array($cat_query,MYSQL_ASSOC);
  $category = $value['cat_name'];

  $sub_query = mysql_query("SELECT sub_name FROM categories WHERE $subcategory_id = ".$ret_val['subcategory_id']);
  $value = mysql_fetch_array($sub_query,MYSQL_ASSOC);
  $category = $value['sub_name'];

  $cat_query = mysql_query("SELECT image_name FROM categories WHERE $ad_id = ".$ret_val['ad_id']);
  while($value = mysql_fetch_array($cat_query,MYSQL_ASSOC)): //loop to get all images for ad
    $image = $value['image_name'];
  endwhile;

//make ads array
  $ad = array(
   'tilte'=>$ret_val['title'],
   'price'=>$ret_val['price]',
   'desc'=>$ret_val['description'],
   'category'=>$category,
   'subcategory'=>$subcategory,
   'images'=>$image
  );
endwhile;
//return completes ad(s)
  return $ad;
}

 

Then I loop through the returned array like this...

include(myClass.php);
$class = new myClass();

$ads = $class->makeAd();

foreach($ads as $ad):
  echo '<ul>'
  echo '<li>'.$ad['title'].'</li>';
  echo '<ul><li>Price: '.$ad['price'].'</li>';
  echo '<li>Category: '.$ad['category'].'</li>';
  echo '<li>Subcategory: '.$ad['subcategory'].'</li>';
  echo '<li>Description: '.$ad['description'].'</li>';
  foreach($ads['images'] as $image):
    echo '<li>Image: '.$image['image_name'].'</li>';
  endforeach;
  echo '</ul></ul>';
endforeach;

 

I get no errors but I do not get correct results either just random letters and numbers for each item.  I am sure it has to do with how I have handled looping through the multidimensional array but can;t sort it out.  Any help/coding advice would be greatly appreciated.  Please also note that the html in the loop is only there for testing/development and is just thrown together.

 

 

Thanks.

 

Mike.

 

 

Link to comment
Share on other sites

I'll try that and see what happens. I also noticed a ton of syntax and other errors in the code I wrote in.  thats what I get for not just copying the code over.  I'll post "working code" shortly hopefully with more correct code someone can help me out.  Sorry about that.

 

Mike

Link to comment
Share on other sites

Here is code that runs without errors:

 

function makeAd($id=null){
//if ad id is given then only get that ad
  if($id != NULL) {
    $where = "WHERE ad_id = $id";
  }
  else {$where = "";}

$sql = "SELECT * FROM ads".$where;
$query = mysql_query($sql) OR die($mysql_error());

//use while loop to make ad
  while($ret_val = mysql_fetch_array($query, MYSQL_ASSOC)):
//get ads category, subcategory and images etc from other tables
  $cat_query = mysql_query("SELECT category_name FROM categories WHERE category_id = ".$ret_val['category_id']);
  $value = mysql_fetch_array($cat_query,MYSQL_ASSOC);
  $category = $value['category_name'];

  $sub_query = mysql_query("SELECT subcategory_name FROM subcategories WHERE subcategory_id = ".$ret_val['subcategory_id']);
  $value = mysql_fetch_array($sub_query,MYSQL_ASSOC);
  $subcategory = $value['subcategory_name'];

  $cat_query = mysql_query("SELECT image_name FROM images WHERE ad_id = ".$ret_val['ad_id']);
  while($value = mysql_fetch_array($cat_query,MYSQL_ASSOC)): //loop to get all images for ad
  	$image = $value['image_name'];
  endwhile;

//make ads array
  $ad = array(
   'tilte'=>$ret_val['ad_title'],
   'price'=>$ret_val['ad_price'],
   'desc'=>$ret_val['ad_long'],
   'category'=>$category,
   'subcategory'=>$subcategory,
   'images'=>$image
  );
endwhile;
//return completes ad(s)
  return $ad;
}

 

Here is the result from print_r

 

Array

(

    [tilte] => Mike's Mundane Blog

    [price] => 99.00

    [desc] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin consequat, mauris sit amet venenatis aliquet, purus elit elementum nisl, non semper nisi augue vel justo. Vestibulum vel augue non tortor commodo porttitor. Mauris lorem est, porttitor vehicula rutrum vel, egestas at purus. Morbi sodales mauris eu mauris pulvinar dignissim. Nullam sodales porta ligula, ac aliquet quam dapibus ac. Cras vel erat eu magna scelerisque tristique. Curabitur ac mi eros, eu scelerisque ipsum. Donec non ipsum et nulla egestas sodales in quis augue. Quisque tempor dignissim nisi eu ultrices. Sed nisl erat, ultricies ac posuere id, interdum vel nunc. Duis a lorem est, sagittis sollicitudin ligula. Nunc sed urna sit amet lorem consequat porta non placerat metus.

    [category] => Electronics

    [subcategory] => Samsung

    [images] => sun.jpg

)

 

There are three ads total each ads has four images.

 

Here is the result when Itry and loop through the returned values

 

<html>
<body>
<ul><li>M</li><ul><li>Price: M</li><li>Category: M</li><li>Subcategory: M</li><li>Description: M</li><li>Image: M</li></ul></ul><ul><li>9</li><ul><li>Price: 9</li><li>Category: 9</li><li>Subcategory: 9</li><li>Description: 9</li><li>Image: 9</li></ul></ul><ul><li>L</li><ul><li>Price: L</li><li>Category: L</li><li>Subcategory: L</li><li>Description: L</li><li>Image: L</li></ul></ul><ul><li>E</li><ul><li>Price: E</li><li>Category: E</li><li>Subcategory: E</li><li>Description: E</li><li>Image: E</li></ul></ul><ul><li>S</li><ul><li>Price: S</li><li>Category: S</li><li>Subcategory: S</li><li>Description: S</li><li>Image: S</li></ul></ul><ul><li>s</li><ul><li>Price: s</li><li>Category: s</li><li>Subcategory: s</li><li>Description: s</li><li>Image: s</li></ul></ul><ul><li>M</li><ul><li>Price: 9</li><li>Category: E</li><li>Subcategory: S</li><li>Description: L</li><br /> 
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> 
</ul></ul><ul><li>i</li><ul><li>Price: 9</li><li>Category: l</li><li>Subcategory: a</li><li>Description: o</li><br /> 
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> 
</ul></ul><ul><li>k</li><ul><li>Price: .</li><li>Category: e</li><li>Subcategory: m</li><li>Description: r</li><br /> 
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> 
</ul></ul>
<body>
</html>

 

Here is the loop I used to get this:

 

foreach($ads as $ad):
	echo '<ul>';
	echo '<li>'.$ad['title'].'</li>';
	echo '<ul><li>Price: '.$ad['price'].'</li>';
	echo '<li>Category: '.$ad['category'].'</li>';
	echo '<li>Subcategory: '.$ad['subcategory'].'</li>';
	echo '<li>Description: '.$ad['description'].'</li>';
	foreach($ad['images'] as $image):
   		echo '<li>Image: '.$ad['image_name'].'</li>';
	endforeach;
	echo '</ul></ul>';
endforeach;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.