Jump to content

Complex problem, delimiter array display of for loop nested inside a while loop


jaxdevil

Recommended Posts

This one takes a big mind to wrap their head around. I get head aches when I do stuff this complex. Ok here is what I am trying to do. I have category and subcategory fields in a field in my database called `cats`, for example, an ipod would be 'consumer electronics|portable audio|mp3 and mp4 players|ipod' and I have below a copy of my code, it is to generate an xml output for google. The code works, but not the displaying the product part. The code I use to display the subcategory works on my main pages of my site, but when I try to add it to this I get no result in the $product_type field. Maybe someone on here can figure it out? Also, the code , you see where I have it subtracting 1? $numberz = $size2-1; on that part? I know that is one thing that is wrong. That counts the number of fields, I need the number  for $numberz to display the number of delimiters in the array return, so when I select the depth to go using the $cats[$numberz] it shows the subcategory for that item. I can't just make it a standard number, it has to be dynamic because some things have a category and are 3 subcategories deep, and others are only 1 subcategory deep. I can tell which one by how many '|' delimiters are in the display. So anyone can figure this one out? Must be Albert Einstein if you can. I am going to take some Excedrin right now..lol!!

 

<?php

$database =  "xxx";
$dbconnect = mysql_pconnect('localhost','xxx','xxx');
mysql_select_db($database, $dbconnect);
$query = "select * FROM `products` WHERE `active`='Yes'";
$result = mysql_query($query, $dbconnect);

while ($line = mysql_fetch_assoc($result))
        {
            $return[] = $line;
        }
$query2 = mysql_query($query);
$seen = array();
$size2 = sizeof($seen);
$numberz = $size2-1;
while($row = mysql_fetch_array($query)) {
    $cats = explode("|",$row['cats']);
    $main_cat = $cats[$numberz];
    if(!in_array($main_cat,$seen) && !in_array($main_cat,$multiple))
    {
        array_push($seen,$main_cat);
    }
    else
    {
        array_push($multiple,$main_cat);
        unset($seen[$main_cat]);
    }
}
$size = sizeof($seen);
for($i=0;$i<=$size;$i++)
{
$product_type = $seen[$i];
}
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<rss version =\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">
<channel>
<title>GS Wholesale</title>
<description>Your Consumer Electronics and More Store!</description>
<link>http://GSWholesale.com</link>";
            
foreach ($return as $line)
{
    $output .= "<item><title>".htmlentities($line['man'])." ".htmlentities($line['mod'])." ".htmlentities($line['name'])."</title>

<g:brand>".htmlentities($line['mod'])."</g:brand>
<g:condition>new</g:condition>
<g:description>".htmlentities($line['specs']).". About the manufacturer ".htmlentities($line['man'])." ".htmlentities($line['full'])."</g:description>
<g:model_number>".htmlentities($line['mod'])."</g:model_number>
<g:id>".htmlentities($line['mod'])."</g:id>
<g:image_link>".htmlentities($line['img_url'])."</g:image_link>
<g:pickup>true</g:pickup>
<g:link>http://GSWholesale.com/product.php?id=".htmlentities($line['mod'])."</g:link>
<g:mpn>".htmlentities($line['mod'])."</g:mpn>
<g:price>".htmlentities($line['price'])."</g:price>
<g:product_type>".$product_type."</g:product_type>
<g:upc>".htmlentities($line['upc'])."</g:upc>
<g:quantity>1000</g:quantity>
<g:payment_accepted>Google Checkout</g:payment_accepted>
<g:payment_accepted>PayPal</g:payment_accepted>
<g:payment_accepted>Visa</g:payment_accepted>
<g:payment_accepted>Mastercard</g:payment_accepted>
<g:payment_accepted>American Express</g:payment_accepted>
<g:payment_accepted>Discover</g:payment_accepted>
<g:payment_accepted>Cash</g:payment_accepted>
<g:payment_accepted>Checks</g:payment_accepted>
<g:payment_accepted>Cashiers Checks</g:payment_accepted>
<g:payment_accepted>Money Orders</g:payment_accepted>
<g:payment_accepted>Wire Transfer</g:payment_accepted>
               </item>";
}
$output .= "</channel></rss>";
header("Content-Type: application/rss+xml");
echo $output;
?>

I think this is too complex. I am going to post a new topic but for a different approach. I can't figure this out and I don't expect anyone on here to go through this kind of a physical pain headache as this takes. I mean for one like this you really have to keep like 8 things in your mind at all times. Too much. Must be a simpler way. Lets see. I am leaving this one because if someone can modify this code that would be great.

<?php

$database =  "xxx";
$dbconnect = mysql_pconnect('localhost','xxx','xxx');
mysql_select_db($database, $dbconnect);
$query = "select * FROM `products` WHERE `active`='Yes'";
$result = mysql_query($query, $dbconnect);

while ($line = mysql_fetch_assoc($result)) {
    $return[] = $line;
}

$query2 = mysql_query($query);
$seen = array();

$size2 = sizeof($seen);
$numberz = $size2;

while ($row = mysql_fetch_array($result)) {
    $cats = explode("|",$row['cats']);
    $main_cat = $cats[$numberz];
    if (!in_array($main_cat,$seen) && !in_array($main_cat,$multiple)) {
        array_push($seen,$main_cat);
    } else {
        array_push($multiple,$main_cat);
        unset($seen[$main_cat]);
    }
}
$size = sizeof($seen);
for ($i=0; $i<=$size; $i++) {
    $product_type = $seen[$i];
}
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<rss version =\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">
<channel>
<title>GS Wholesale</title>
<description>Your Consumer Electronics and More Store!</description>
<link>http://GSWholesale.com</link>";

foreach($return as $line)
{
    $output .= "<item><title>".htmlentities($line['man'])." ".htmlentities($line['mod'])." ".htmlentities($line['name'])."</title>

<g:brand>".htmlentities($line['mod'])."</g:brand>
<g:condition>new</g:condition>
<g:description>".htmlentities($line['specs']).". About the manufacturer ".htmlentities($line['man'])." ".htmlentities($line['full'])."</g:description>
<g:model_number>".htmlentities($line['mod'])."</g:model_number>
<g:id>".htmlentities($line['mod'])."</g:id>
<g:image_link>".htmlentities($line['img_url'])."</g:image_link>
<g:pickup>true</g:pickup>
<g:link>http://GSWholesale.com/product.php?id=".htmlentities($line['mod'])."</g:link>
<g:mpn>".htmlentities($line['mod'])."</g:mpn>
<g:price>".htmlentities($line['price'])."</g:price>
<g:product_type>".$product_type."</g:product_type>
<g:upc>".htmlentities($line['upc'])."</g:upc>
<g:quantity>1000</g:quantity>
<g:payment_accepted>Google Checkout</g:payment_accepted>
<g:payment_accepted>PayPal</g:payment_accepted>
<g:payment_accepted>Visa</g:payment_accepted>
<g:payment_accepted>Mastercard</g:payment_accepted>
<g:payment_accepted>American Express</g:payment_accepted>
<g:payment_accepted>Discover</g:payment_accepted>
<g:payment_accepted>Cash</g:payment_accepted>
<g:payment_accepted>Checks</g:payment_accepted>
<g:payment_accepted>Cashiers Checks</g:payment_accepted>
<g:payment_accepted>Money Orders</g:payment_accepted>
<g:payment_accepted>Wire Transfer</g:payment_accepted>
</item>";
}
$output .= "</channel></rss>";
header("Content-Type: application/rss+xml");
echo $output;
?>

 

 

you were trying to use mysql_fetch_row, on a query string. not an actual query. I doubt this alone will fix your issue. but good luck.

i don't real understud what you doing

if you just want to extract last part of string try

<?php
$a = 'consumer electronics|portable audio|mp3 and mp4 players|ipod';
$b = explode('|',$a);
echo $numberz = count($b) - 1;
echo ' last element is: ',$b[count($b) - 1];
// or
//echo $numberz = preg_match_all('/\|/',$a,$b);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.