Jump to content

Manipulating (cutting) Array Data and Output Size


vbcoach

Recommended Posts

Hello all.

I have a sports league database where when a team registers as a “returning team” from last year, it queries a table that contains the basic information from last year’s team names, league, night and division.  Simple enough.  However I would like to do two things to the data in the array. 

 

First, I want to take two fields ‘firstname’ and ‘lastname’ and create an output that lists only the first character of the ‘firstname’, add a dot, then the ‘lastname’ i.e. J. Doe as the output.

 

Second, I want to limit the number of characters in the ‘teamname’ field to no more than 25 characters, and if it’s over 25 characters, limit the output to 25 characters then 3 dots ‘…’  For example if the teamname was “Twenty Five Crazy Characters Team” it would be cut to “Twenty Five Crazy Charact…” to limit the size of the field I am using.

 

I basically only have 5 strings I am working with.  “season” “league” “teamname” and “firstname” and of course “lastname”.  I am simply having the hardest time manipulating the array data to output as I have written above.  I have hit a wall here.  Was hoping some type of substr() statement would help, but alas it is not working.  PHP coders, I NEED YOUR HELP!

 

Thanks in advance!

<select name="ret_teamname" id="ret_teamname" tabindex="4" >
      
      <option value="" selected="selected">-- Select Returning Team  --</option>
      <?php  while ($lrow = mssqlfetchassoc($rres))  	 {    ?>
     
      <option value="<?php echo $lrow['l_id'];?>"><?php echo "$lrow[season] $lrow[leaguename] $lrow[rteamname] $lrow[firstname] $lrow[lastname]"; ?></option>
      <?php
		 } 
		 ?>
    </select>
Link to comment
Share on other sites

You don't really need to worry about dealing with arrays here. Both of the things you want to do only manipulate strings - it's just that the strings you want are inside of an array.

 

1. The first character of a $string is easy to get using $string[0]. Works just like an array. And yes, it also works with things besides a simple variable.

$firstnameinitial = $lrow['firstname'][0];
2. strlen() tells you the length of a string. Use an if to set a new variable with the displayable name of the team according to your rule. Then substr() to get a substring.

if (strlen($lrow['rteamname']) > 25) {
	$teamname = substr($lrow['rteamname'], 0, 25) . "...";
} else {
	$teamname = $lrow['rteamname'];
}
Now use those two new variables in your echo.
Link to comment
Share on other sites

I could use some help here.  I was unable to get any variable to echo without the $lrow[ ] statement.

 

Right now my code looks like this:

<?php  while ($lrow = mssqlfetchassoc($rres))  	 {    ?>

      <option value="<?php echo $lrow['l_id'];?>"><?php echo "$lrow[season] $lrow[league] $lrow[rteamname] $lrow[firstname] $lrow[lastname]; ?></option>

How would the above statement look and be placed inside of this code?

 

Thanks!

Link to comment
Share on other sites

Notice the syntax Requinix is using with your associative array key names.  You need quotes around the key name.  

echo "Season: $lrow['season'], League: $lrow['league'], Team Name: $lrow['rteamname'], First Name: $lrow['firstname'], Last Name: $lrow['lastname']";

Note you can only output your variables without concatinating a long string when your variables are enclosed in double quotes:

// doesn't work
echo "Season: $lrow["season"], ...";

// doesn't work
echo 'Season: $lrow['season'], ...';

// does work, concatinated string
echo "Season: " . $lrow["season"] . ", ...";

//does work, concatinated string
echo 'Season: ' . $lrow['season'] . ', ...';

// does work, mixed quotes
echo "Season: $lrow['season'], ...";
 

Finally, to make sure your array has value use var_dump($array_name) or print_r($array_name) to output the arrays content.

 

Good Luck

rwhite35

Edited by rwhite35
Link to comment
Share on other sites

Sorry correction to the above:

// does work
echo "Team Name: " . $lrow['rteamname'] . ", Season: " . $lrow['season'];

//does work
echo "Team Name: $lrow[rteamname], Season: $lrow[season]";

// doesn't work
echo "Season: $lrow['season'], ...";

regular scalar variables like $teamname wouldn't require concatinating or escaping when using double quotes around the echo statement.  Mixed quotes like above do require one syntax or the other.

Edited by rwhite35
Link to comment
Share on other sites

By the way, I do not actually need the identifiers like "season" or "teamname" to preceed the output.  For my team captains who will be seeing this information, it's already self-explanatory.  Besides, I have limited space in the webform for the output - hence the need to limit the size of the teamname.  Thanks!

Link to comment
Share on other sites

Consider using a template engine like Twig instead of prehistorical PHPHTML spaghetti code with dozens of security vulnerabilities. This allows you to render your HTML in a sane way:

<select id="ret_teamname" name="ret_teamname" tabindex="4">
    <option value="" selected>-- Select Returning Team --</option>
    {% for member in team_members %}
        <option value="{{ member.l_id }}">{{ member.season }} {{ member.leaguename }} {{ member.rteamname|truncate(25) }} {{ member.firstname|first }}. {{ member.lastname }}</option>
    {% endfor %}
</select>

(the truncate() filter requires the Text extension)

 

Unlike your code,

  • this can actually be understood by human beings, not just the PHP interpreter
  • it's secure, because all input is automatically HTML-escaped
  • it separates the business logic from the visual presentation, making it a lot more readable and extensible
Edited by Jacques1
Link to comment
Share on other sites

Well, great idea - and I am always open to doing things an easier way.  However your statement above absolutely doesn't work - at least not in my environment.  It has no idea what 'member.' is, or how this is defined.  It also eliminated my "<?php  while ($lrow = mssqlfetchassoc($rres))   {    ?>" loop.  I'm lost here.

Link to comment
Share on other sites

By the way, I am also just using DreamWeaver to code and upload to the server.  We are using a 3rd party shared hosting provider, thus we have virtually zero access to anything other than uploading edited pages.  Not the best solution, obviously, but it's what we have to work with for now.  So for now, Twig is out for me :(

 

I have a relatively simple pull-down option statement:

<select name="ret_teamname" id="ret_teamname" tabindex="4" >
      
      <option value="" selected="selected">-- Select Returning Team  --</option>
      <?php  while ($lrow = mssqlfetchassoc($rres))  	 {    ?>
     
      <option value="<?php echo $lrow['l_id'];?>"><?php echo "$lrow[season] $lrow[leaguename] $lrow[rteamname] $lrow[firstname] $lrow[lastname]"; ?></option>
      <?php
		 } 
		 ?>
    </select>

... that needs to be able to display properly the manipulated strings.  ANYONE PLEASE?

Link to comment
Share on other sites

I am not a coder by trade.  I placed it in what seemed to be the most logical places, and it did not work.  Also guru mentioned that the "case" convention you gave me would not work as written.  Not trying to be a pita here, but statements like yours for someone reaching out for help is very frustrating.

Link to comment
Share on other sites

Let's get some things straight: You got plenty of help and a large variety of solutions to choose from. You rejected all of them, merely telling us that they are not an option or “don't work”. This is entirely unproductive and, obviously, doesn't get you anywhere. Maybe you thought that eventually somebody would write the code for you, but that's not what this forum is about. We expect people to actively work on the problem. Your code doesn't work? Show the code.

 

Maybe you should concentrate less on complaining and more on getting things done.

Edited by Jacques1
  • Like 1
Link to comment
Share on other sites

Let's get one more thing straight!  I use this forum for two purposes:  To ask for help with an issue that I cannot otherwise figure out for myself, and to actually "learn" something along the way.  These two go hand-in-hand.  My request was simple, and I appreciated all the help along the way.  Yet when I take someone's  "suggestion" which is incomplete or doesn't work for me - and then ask for additional assistance as to "why", I really don't take kindly to what you call "unproductive" answers.  I have shown my code all along, and simply asked for assistance in WHERE to place the suggested item responses I received here to make it all work.  As stated, I tried myself in what I considered 'logical locations' but I am not an expert coder, or I wouldn't be here.

 

Guru, you could have easily answered my question rather than this bickering and bantering back and forth.  I love this place.  Been here for years.  Fortunately I do not have to come here all that often, but when I do - it is because I am "stuck" on something and really need some guidance - and yes at times to have someone write their suggestions for me into a usable code (my code) so I can figure out why mine didn't work, and what the proper method was.  That was my intent here.  A simple "I would placed this code 'here' inside your code" would have sufficed.  To date, no one has actually directed me at to where this simple suggestive code should be placed.  Now is that really this difficult?  You could have also placed this in a private message versus blabbing this to the entire PHPFREAKS world.  Now that is not good for either of us.

Link to comment
Share on other sites

Please don't make me lock this thread, guys.

 

Yes, we could have easily answered the question. I (or anyone) could have posted the answer, you would have copied it, seen it working, and been on your way. But would you have learned anything? Not really. Well, maybe: it's possible you're one of those types who will take code given to them and try to understand what it does, but in my experience the majority (not everyone) of people who ask programming questions on the internet are not like that.

 

So I gave you a couple bits of code that will (should) get you most of the way to the answer. rwhite35 talked about putting variables in strings. Jacques1 suggested you use a templating engine so you don't have to deal with embedding PHP code into HTML. But not even all of that combined would not have given you the complete solution. And that's the point.

 

The normal process is that someone posts some code to help, OP comes back saying it doesn't work, we ask how it doesn't work and what their code is, they post, we tell them what they're missing, they figure it out, and we're done. Sometimes repeating those last couple steps a few times. Hopefully OP learned what was going on so that next time they come across a similar problem they will be able to say "oh, I recognize this situation from a problem I had earlier - I wonder if the same sort of solution will work for it too". We avoid giving out answers not because we want to annoy but because we want people to learn, and being fed answers doesn't really do it. Unfortunately not everyone understands that, and those people will spend months or years coming back asking simple questions because they aren't learning - or worse they quit after a couple threads because we didn't tell them the answer and they get frustrated and ragequit.

 

You seem to get that we're trying to help you learn. Yes, sometimes threads get sidetracked and sometimes the discussion gets heated and sometimes people get offended. It happens because we're human. How about we take a step back, reconsider what this thread is supposed to be about, and go back to where we left off in that normal process?

 

You have some code that isn't working. If you only put the code I posted into what you had then you're still missing something to make it work. So: what is the code you have now and how is it not working?

Edited by requinix
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.