got this far, now how to get one random line?

Have a little csv file I want to pull specific and random bits of data and display on a web page from and admittedly very novice with PHP.

I have gotten this far:

[php]

<?php $handle = fopen("jour.csv", "r"); $row = 1; $line_of_text = array(); while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) { echo $data[6]; echo ""; } ?>

[/php]

And have gotten it to the row of data - now I just want to pull random entries and cannot seem to really pin down how to do it.

Any and all suggestions/help will be appreciated.

~mrc

You could use rand(). Maybe something like:

[php]
$max = count($data);
$id = rand(1, $max); // Generate a random number between 1 and the total # of array elements
echo $data[$id];[/php]

That works well, but I only want to pull random data from one row [6], not all rows. This is what is stumping me…

Post an example of what the data in a row looks like… you can probably just explode() on a character and then use the rand() example I posted.

OK -I posted about 10 lines of the csv so you can see what i’m working with - it is survey data from a training course. We want to pull random comments, for example from the 5th column (How did you spot your leadership challenge), and randomly display one at a time in a spot light section on a web page.

With the orginal post, I’ve got it to spit out all the data for that question from the file, now I just want one random line from that?

I’m probably making no sense. :-[

Last_name,First_name,Site_Steering,What leadership problem or opportunity of yours will you address?,How did you spot your leadership challenge?,Leadership Comment,What SkillSoft learning resource(s) did you find to help tackle your leadership challenge?,What actions will you take?,What do you hope will improve by your actions? ,What leadership problem or opportunity of yours did you address?,What actions did you take? ,What improved by your actions? ,What reflections do you have as to how you improved as a leader? ,What advice do you have for others who want to do something similar?
Doe 1,John1,East Longmeadow,I would like to become better at getting people to co-operate and work together to achieve their goals.,Found a pressing challenge in my work.,I used various resources in the Skillsoft program.,I will take what I have read in Skillsoft and put it to use in my day tot day activities on the floor.,I hope by putting what I have learned in skillsoft to use on the floor it will help my team work together more cohesively and will help them reach the goals they are trying to achieve.,Making people work together and get along to achieve a goal.,“I gave a team meeting and expressed the importance of working together and comradery. I explained to everyone in the room the importance of our goals as a company and what we need to do to achieve them. We need to work as a team to reach our goals (Safety, Quality, and Production). This includes understanding each others problems and helping each other work through them to complete there day.”,I started seeing people on the floor start to lend each other a hand more often. I even witnessed a few people that I knew didn’t get along before actually help each other out a couple times through out the night.,I realized as a leader I have a lot of influence on the people that work for me and just by talking to them I can change what is going on inside the plant.,Before you give a meeting or you sit down to talk to an individual make sure you go over all of the materials you are going to cover before you give the meeting.
Doe 2,John2,East Longmeadow,Coaching,Found a pressing challenge in my work.,“Great information on coaching. Fast Track - Coaching The Coaching Manager Developing Top Talent in Business - Book by James Hunt & Joseph Weintraub Videos’ - Marcus Buckingham, Dick Heller, Marshall Goldsmith”,I will use these techniques I’ve learned with the person that has presented a challenge for me.,Developing a better relationship with this individual so I can help coach the person and they can work of areas of their development.,Coaching someone that has been difficult to coach because of their lack of follow through.,“1. First, I confronted the person and discussed why the follow-through was important, I focused my story around others people were involved and depending on this person to complete there part. 2. Set and agree on new target dates 3. I’m Assisting the person and working within a range of mentor & coach”,The skill to see exactly what the problem was,“Well, no matter what position a person holds, sometimes you have to be flexible and provide them with a wide range of your skills, make yourself available and think about taking baby steps, walk that person through and be ready to also mentor not only coach”,“Take the time and read/study the coaching process, perhaps, for some with less experience practice, be patience, be an active listener, and it’s very important to know the business case around the situation”
Doe 3,John3,Medina,Onboarding myself as a new Manager.,Found a pressing challenge in my work.,All members of my Skillsoft Group were either onboarding or about to onboard new associates and this was selected as our leadership challenge.,book: The New Boss - How to Survive the First 100 Days,Patience in setting expectations for self and associates. Determine what problems are important and which to tackle first.,Acceptance as a Management Team Member and boss.,Onboarding myself as a new Manager,Read excerpts from the book The New Boss How to Survive the First 100 Days,Defining and managing expectations of myself and subordinates. Figuring out what problems are important and which to tackle first. Common obstacles to avoid to ensure a successful transition.,“In defining expectations, ask subordinates what they hope their new manager can do for them. Do not present new ideas too quickly. New managers should always be sure to formulate at least one stability-related goal for every three goals related to change. Ask questions differently to foster a positive climate for change. First ask ““reflective questions”” to get people to think, or brainstorm. Next ask ““resourceful questions”” intended to help identify strengths. Do not ask about the cause of problems.”,I would recommend reading this book to managers in the first 100 days of a new assignment.
Doe 4,John4,Pineville,time managment,Found a pressing challenge in my work.,plan task by priority,pay more attention to deadlines,stay focused better on the current task when deadlines arent in place,time management,havent worked on this,n/a,n/a,it could be a good program for someone who sits at a desk all day. for someone like me that is always on the floor fixing problems and fighting fires its hard to get motivated with this.
Doe 5,John5,Pineville,Coaching direct reports,Somebody else’s Achievement Story was related to a work challenge of mine.,We had different individuals in Pineville that held presentations for groups that they believed would be relevant and this is one I felt I could gain from the most.,Coaching: Fast Track -,During evaluations - have each direct report pick one process that they will learn in depth and I will help use coaching skills to assist them in this task.,“My direct reports will gain more knowledge about processes in the plant. Depending on the process chosen, that will either help in communication with the floor and possibly reduce quality incidents or it will help us build our pool for other positions if they come open.”,Coaching direct reports,During evaluations I spoke with each individual about choosing one process that they would be interested in improving upon. I explained the importance of continual improvement and how this could help themselves and the company. I put reminders in my calendar to follow up with these individuals at specified time frequencies as this is a crucial part of coaching.,“Unfortunately, this process will take a while to complete so all improvements have not yet been seen but my direct reports did respond to the idea very positively and look forward to the opportunity to grow.”,Developing your direct reports is an important part of managing. It is how Plastipak will continue to succeed in the future. Coaching is an important part of this process and following up on agreed upon action plans is a must. This is something that I have sometimes failed to do in the past but will hopefuly do better now with the help of skillsoft.,Look at what you are doing wrong with coaching first before you look at why the coachee is not understanding the message.
Doe 6,John6,East Longmeadow,Managing Time. Making better use of my time and learning to delegate tasks but still get the results needed,Found a pressing challenge in my work.,i also received some feedback from my manager on how I make use of my time.,“There is a book called ““The Concise Time Managment and Personal Develpopment”” that hits on a lot of what I was looking for. I actually got the idea to work on delegation as part of my Action Plan based on one of the chapters”,“I’ll start outlining a list of daily tasks and listing them in order of importance. This way I can get an overview of items I need to do and what can be ““farmed”” out. Looking at my supervisors strengths and utilizing them to complete some of these items”,Better follow through on my assigned duties. More productive time versus just plain time spent working. Giving my supervisors more buy in on things that affect the day to day operation.,I was working on Time Managment. I’m looking to make better use of the time I have at work so I don’t need to take it home or stay late or come in early to complete things that I should be able to do when I’m here.,“When I started looking around Skillsoft it led me to several resources, one of which was a book caled ““Concise Time Managment””. One area was dedicated to delegation and it made me realize that not all work assigned to me was necessarily something I had to work on alone. I was able to prioritze my tasks and look at my resources and then use the strengths of others to help me out. This wasn’t to pass the buck but rather use the skills available to accomplish assignments. I also was able to look at daily tasks and work out a schedule. By blocking time for certain things I’m able to use this to my advantage by not saving everything to the last minute”,“I was able to improve the timliness of certain day to day things like inventory, invoices and personell issues simply by blocking time for them. Obviously curveballs come up and need immediate attention but I feel that by scheduling events rather then reacting right away I’m able to accomplish more”,I know I still have some work in this area but in the past week I feel as though I have made progress. I’m sure as I work on this further I will see additional improvements,“Really take a look at if what you do all day is relly necessary. It’s easy to say ““I’m busy”” but you have to take a look at what you’re busy with.”
Doe 7,John7,Pineville,“I took the opportunity to take control of the completion & flow of hard data, ““paper work”” from an overburdened coworker.”,Found a pressing challenge in my work.,“Confrontation, Delegation & Workplace ethics.”,“Confronted felow employee about an abillity hurdle, ““Time managment””. Delegated his task to another employee who had the time & recources to coplete the task. Did so in a professional maner privately.”,Improve my coworkers time managment awarness.,Confrontation of coworkers time managment skills.,Confronted coworker and found solution.,Communication and coworkers awarness of priority.,I found another way that I lead by example.,“Dont make accusatoins, be nice & offer to help without trying to take controll.”
Doe 8,John8,East Longmeadow,Preparing for LOA through delgating work load.,Found a pressing challenge in my work.,“3 books, Trends, Reflections, Career Management and Musing, Chapter 9 Everything Managers People Book, Chapter 7 Delegating for Business Success”,“I will read through the chapters and try to find the best approach to ask for help, train, and delegate my workload.”,I hope I can prepare for a smooth transition when I go on leave.,I am going to be out on medical leave and need to delegate and train others to complete my duties. I wanted advice on how to delegate without offending others by giving them MORE work and also how to train them quickly but thoroughly.,“I explained my situation to those who would be affected. What I learned in the skillsoft book I read was that if you are honest and explain the situation to people, they are usually more cooperative. I started by explaining it was a medical leave and that I was sorry to put the burden on them, but I wanted to start training them immediately so they would have adequate time to learn the duties.”,“I beleive my approach made others more willing to help. They have all been great and keep telling me not to worry, they will take of everything while I am gone. Some have evened asked what else they can do to help.”,I feel like I thought through the process before approaching others. I often plow in like a bull and don’t always present things they way I would have liked. I took a few days to think about how to approach the situation then proceeded calmly and slowly.,“Honesty is definitely a plus. If others undertand where you are coming from, they are more inclined to buy into whatever it is you are trying to do.”

Hmm… ok, I think I’m following you now. Working with your original code, lets create another array to just capture the columns you want to display:

[php]

<?php $handle = fopen("jour.csv", "r"); $row = 1; $line_of_text = array(); $comments = array(); while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) { $comments[] = $data[6]; } $max = count($comments); $comment_id = rand(1, $max); echo '

' . $comments[$comment_id] . '

'; ?> [/php]

Is that it?

Oh, btw… we’re neighbors - I live in AA.

AWESOME!

I can finally put the rest of this thing to bed! It seems like such a small thing, but to a non-programmer who couldn’t get IT to take a look, this means much to me.

Now, just to tweak the thing…thanks!!!

You’re welcome :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service