HCProfessionals Posted April 3, 2012 Share Posted April 3, 2012 I'm trying to add options to products. Currently, I have them in this format: Leather reinforced hilt,5.00::Stabbing tip,3.00::Logo,10.00 $option_results = mysql_query("SELECT product_options FROM products WHERE product_id='".$product."'"); while($option_row = mysql_fetch_array($option_results)){ $option = explode("::", $option_row[0]); $option_part = explode(",", $option[0]); $option_part[0] = $option_name; $option_part[1] = $option_price; echo "<li><a href=\"#\">".$option_name.", Add $".$option_price."</a></li>"; } I would like the output to be: <li><a href="#">Leather reinforced hilt, Add $5.00</a></li> <li><a href="#">Stabbing tip, Add $3.00</a></li> <li><a href="#">Logo, Add $10.00</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/ Share on other sites More sharing options...
smerny Posted April 3, 2012 Share Posted April 3, 2012 you are setting $option_part[0] to $option_name which has not been set... looks like you want to do the opposite... same with $option_price Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333936 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2012 Share Posted April 3, 2012 After the explode, $option is an array. You would need to loop over that array and process each entry in the array. Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333939 Share on other sites More sharing options...
freeloader Posted April 3, 2012 Share Posted April 3, 2012 Also, my escaping is a bit off, but putting the $ sign there blank between double quotation marks (") seems dangerous. Use single quotations (') for this, they also process slightly faster. Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333940 Share on other sites More sharing options...
scootstah Posted April 3, 2012 Share Posted April 3, 2012 but putting the $ sign there blank between double quotation marks (") seems dangerous It won't hurt anything, because there is no valid variable there to parse. Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333943 Share on other sites More sharing options...
HCProfessionals Posted April 3, 2012 Author Share Posted April 3, 2012 I'm not really getting anywhere... $option_results = mysql_query("SELECT product_options FROM products WHERE product_id='".$product."'"); while($option_row = mysql_fetch_array($option_results)){ $sep_option = explode("::", $option_row[0]); foreach ($sep_option as $sep_result) { $option_part = explode(",", $sep_result); $option_part[0] = $option_name; $option_part[1] = $option_price; echo "<li class=\"product\" id=\"option-".$option_name."\"><input class=\"purchase\" type=\"button\" value=\"Add to cart\"><span class=\"title\">".$option_name."</span>, Add $<span class=\"price\">".$option_price."</span></li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333946 Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 As was said before, you're not defining $option_name or $option_price. You're doing those statements backwards, you're saying that the first key in $option_part should be set to the value within $option_name. $option_name does not exist. Switch it around. Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333948 Share on other sites More sharing options...
HCProfessionals Posted April 3, 2012 Author Share Posted April 3, 2012 As was said before, you're not defining $option_name or $option_price. You're doing those statements backwards, you're saying that the first key in $option_part should be set to the value within $option_name. $option_name does not exist. Switch it around. Wow, totally missed that. Thank You!!!! Quote Link to comment https://forums.phpfreaks.com/topic/260260-php-explode-conundrum/#findComment-1333952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.