Jump to content

Recommended Posts

I am attempting to create a comma delimited string that I can convert to an array using explode();.

 

I have the following code:

 

$cPath_new_a = tep_get_path($categories['categories_id']);
$cPath_new_b  = str_replace("cPath=", "", "$cPath_new_a");
$cPath_new_c  = str_replace("_", "", "$cPath_new_b");
$cPath_new_d = substr("$cPath_new_c", -2, 2); 

$cPath_new_e = $cPath_new_d . ",";

echo $cPath_new_e;

This outputs 23,53,

 

I need it to output 23,53

 

Using the substr($cPath_new_e, 0, -1); and/or rtrim() method to remove the last character

for some reason removes all of the commas.

 

Am I approaching this the right way?

 

I really appreciate any feedback.

 

Link to comment
https://forums.phpfreaks.com/topic/56388-need-some-help-with-code/
Share on other sites

$cPath_new_e = $cPath_new_d . ",";

 

this line should go before you add a new element not after you have added one.

it should be something like:

(add ",") data

and not the other way arround:

data (add ",")

if you don't do this it will always add "," at the end of every piece of data that you add, and you need is to add one only if there is one new piece of data to be appended.

 

don't know if this is your problem but I think it would help.

hope it helps

Thanks to those who replied.

However I still have not figured this out. To simplify things I have shortened the code to this

 

$cPath_new_a = $categories['categories_id']. ",";

$cPath_new_b = $cPath_new_a;

 

$cPath_new_c = rtrim($cPath_new_b , ',');

 

echo $cPath_new_c;

 

Still running into that issue. Let me try to explain things:

$categories['categories_id'] pulls a sub category ID out of the database based on what main category the user is in. The example I have been using is a main category with 2 subcategories 23 and 53. $categories['categories_id'] alone echos 2353. I want to make this into an array that contains 23 and 53.

 

I thought the best way to accomplish this would be to add a , to make the string 23,53, then remove the last , then use explode(); to convert the comma delimited string into an array. However rtrim/substr both are not working for me. Is there some way to convert $cPath_new_a into an actual string within the code (not a dynamic variable) then apply rtrim or substr? Or am I approaching this whole thing incorrectly?

fully tested ok see the demo below ok.

 

<?php

$a=array("2353","2344","4563");

$word=$a[0];

$test=str_split($word,2);

print_r($test);

$test1=implode( ' ',$test);

$num1=$test[0];

$num2=$test[1];

echo "<br> i wanted this number $num1 and this number $num2 split in an array fomat";

?>

Hi redarrow, thanks so much for your help. I think you put me on the right track with str_split however I cannot get it to work. Here is what I have:

 

$a = $categories['categories_id'];

$split = str_split($a,2);

}
$array = $split;

foreach($array as $tmpval) 
{

Code Goes Here
}

 

However this does not output anything.

 

More info: This is how $categories['categories_id']; is derived:

 

if (isset($cPath) && strpos('_', $cPath)) {
// check to see if there are deeper categories within the current category
      $category_links = array_reverse($cPath_array);
      for($i=0, $n=sizeof($category_links); $i<$n; $i++) {
        $categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "'");
        $categories = tep_db_fetch_array($categories_query);
        if ($categories['total'] < 1) {
          // do nothing, go through the loop
        } else {
          $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
          break; // we've found the deepest category the customer is in
        }
      }
    } else {
      $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
    }

 

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.