Jump to content

How do I remove this last character in an array?


Jeffro

Recommended Posts

I've ran into this before.. I'm sure it's easy for you guys, but I can't figure it out.  Here's my code..

 

echo '{';
while($row = mysql_fetch_array( $result )) {
$mytitle = $row['title'];
echo $mytitle;
echo '|';
} 
echo '}';

 

After the very last echo in the array, it finishes with:  example|}

How can I remove the | after the very last word only (keeping all other instances) making it finish with:  example}

 

Thanks!

Link to comment
Share on other sites

<?PHPSensei To The Rescue

 

  • Option 1 (possibly): What Alex Mentioned, however Alex's option is for an Array, while op is displaying 1 element from the SQL
  • Option 2: Just count the number of rows in the sql, if its the last loop then prevent it from display the "|", see example below

 

 

 

So What Can I Do Sensei?

 

 

$i = 0;
$last_element = mysql_num_rows($sql);
while($row = mysql_fetch_array($sql)){

if($i < $last_element){ print "|"; } $i++;
}

 

i_heart_my_sensei_t_shirt-p235846181907006226qmkd_400.jpg

Link to comment
Share on other sites

Option 1 (possibly): What Alex Mentioned, however Alex's option is for an Array, while op is displaying 1 element from the SQL

 

Um.. what? My solution would work if there is only a single result.

 

Plus, the whole premise of the question is there will be multiple rows returned, otherwise the whole question is pretty moot. You wouldn't need the while loop, and could just do:

 

echo '{' . $the_single_result . '}';

Link to comment
Share on other sites

Alex he is printing the the titles, not storing them inside the while loop, then reprinting them again. Why do that? The OP just wanted to print them inside the while loop, and leave out a "|" at the end of the last row.

 

 

$the_single_result 

 

Thats not a flexible solution, and if he wanted to edit some row data individually he would after to explode the single result again.

 

 

Link to comment
Share on other sites

I think it's flexible. I was only arguing that my solution would produce the same output, and would work for even a single result, but I would also argue that my solution is just as flexible, and maybe a bit more clean.

 

If you wanted to edit the data, you would do it beforehand, that's kind of the point. You separate your pre-processing from the outputting, it makes it cleaner and gives your output a single point instead of all over the place.

 

$titles = array();
while($row = mysql_fetch_array( $result )) {
$titles[] = $row['title'];
// edit title here before inserting it into $titles.
} 
echo '{' . implode('|', $titles) . '}';

 

The logic behind your code and the output are two completely different things, and it's much cleaner and easier to read if you keep them separated. Instead of having tons of echos everywhere and having to look through 50 lines of code to piece together what happens when the echos get combined, you can simply look at a single line and see it right there.

Link to comment
Share on other sites

In regards to our arguments, nothing personal but I guess you think of it more in the long run whereas I was proposing a quicker example. My simple question that the Data can be edited on the spot and printed out, but whereas I was mentioning that if the user needs to edit $single_items, he would need to count the number of elements, do a for loop, and such.

 

I'm not really sure what you mean. Maybe I didn't make my point clear enough. The whole concept behind my idea is that you keep the data separate from the output. I wouldn't suggest that you format the data in the string, then have to edit it like that later on, that's unnecessary. But if you keep the data separate from the formatting, then you can change the formatting however you want in the future without having to deal with the logic. This is pretty much the whole idea behind template engines.

Link to comment
Share on other sites

Which is why I told OP the exact same thing Alex, however in his case, he just wanted to simply print out the titles to the screen. Your method of data handling would require same amount of work as mine, but since the OP has pm'ed me several times about what he is trying to do, I just posted a more suitable solution to his problem. I doubt he is doing anything OOP related either, he said he is a beginner in his pm's.

 

Your right though, keeping the content seperate from the output would be better, but based on his level of training, he has trouble passing with much simpler things.

 

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.