#1
  1. The Signal:: It's everywhere!
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2007
    Location
    Sydney
    Posts
    549
    Rep Power
    497

    Red face Not sure how to build this loop...


    So I've been trying to think of the best way to output some events to the user based off their usr group.

    I have 3 groups, Managers, Supervisors, Employees.
    I can easily output the events that are for each usr group, I also have it so that each users see the events for their group, and any group that is below their class.

    Use firefox for the links below, I haven't finished writing the IE style sheet.
    Employee see Employee events; 1 Event
    URL: Quidel Univ
    User: 4006

    Supervisors, see Supervisor and Employee events; 2 Events
    URL: Quidel Univ
    User: 1751
    Managers sees Managers, Supervisors and Employee events; 5 Events
    URL: Quidel Univ
    User: 2097

    All this works fine.
    So what I would like to do, is when, lets say a "Manager, logs in.
    They see all those events in one row. I wan to be able to separate the events by the groups separated by some <h1> tags, or something similar.

    so:
    Code:
    <p><h1>Events for Managers</h1>
    Developing Employees at Quidel.......
    What is Career Development at Q....
    etc..<?p>
    <p><h1>Events for Superviros</h1>
    Developing Employees at Quidel.......
    What is Career Development at Q....
    etc..<?p>
    <p><h1>Events for Employees</h1>
    Developing Employees at Quidel.......
    What is Career Development at Q....
    etc..<?p>
    I'm trying to avoid multiple queries, and extra code where I don't need it. I just can't get my head around how to do this properly.

    I was thiking a I would output all the eventID's and ejnAID (AccessID) into an array, then jump through these with a for each statement, where the 'each' is the ejnAID(AccessID). Then I use the eventID so I can call the info I need to display properly.

    But I'm not sure the above ^^ is even a legit way to do that. Havent worked with foreach before. I've been trying a few different scencarios, playing with this, but I get errors and weird output, which makes me think I I was doing it wrong. So I went with the easy for now, which is what you saw if/when you logged in at the links above.

    This is the SQL statement I am using to get the info for the event index page, and the sidebar:
    SQL Code:
    $ejSQL="SELECT * from events as e, events_jn as ej WHERE ej.ejnAID >=".$myACC." AND e.eventID = ej.ejnEID";
    $myACC is a sort hand var for a session variable, which the session variable is pulled from a mysql statement.

    I'm pretty much at a loss, so any insight, or help that can be tossed in my direction, that be cool!

    Thanks dudes!

    LATES!


    edit::
    Yikes, sorry for the novel!
    Last edited by zer0efx; May 19th, 2008 at 11:24 PM. Reason: whoops
    Check out my Blog site if you want a good laugh, Google travelingwebguy or..
    The Official TravelingWebGuy
    Business site => New Blog Site
  2. #2
  3. Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Location
    London, UK
    Posts
    4,005
    Rep Power
    2795
    Why not just drop the results into three strings using an if/else statement, then echo them out?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]
  4. #3
  5. c0der
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2005
    Location
    Vancouver
    Posts
    664
    Rep Power
    159
    Yikes, sorry for the novel!
    No, thank you for the "novel". It is a definite step up from the typical "site broke, help pls" posts we get around here. When in doubt, I'd say longer and more detailed (without being ponderous) is better.

    I'm not sure I'm totally clear on what you're shooting for, but I'll take a shot at answering your question anyhow.

    First, I'd order the results of that SQL query:

    Code:
    $ejSQL="SELECT * from events as e, events_jn as ej WHERE ej.ejnAID >=".$myACC." AND e.eventID = ej.ejnEID ORDER BY ej.ejnAID, 1";
    I'd also include the event type and title in the query results:

    Code:
    $ejSQL="SELECT *, ej.ejnAID as event_target_id, ej.ejnTitle as event_target_title from events as e, events_jn as ej WHERE ej.ejnAID >=".$myACC." AND e.eventID = ej.ejnEID ORDER BY ej.ejnAID, 1";
    Then I'd loop through each result in a while/records loop to build a list:

    php Code:
    $last_event_type = NULL; // Keep track of what the last "person type" id is
    while ($row = mysql_fetch_assoc($result)) {
      if ($last_event_type != $row['event_target_id']) {
        if ($last_event_type !== NULL) { // If there wasn't a person type id before this record, we don't have to close any lists
          echo "</ul>\n\n";
        }
        $last_event_type = $row['event_target_id']; // whatever person type we're looking at now is the new old
        echo "<h1>Events for <strong>" . htmlspecialchars($row['event_target_title']) . "</strong></h1>\n"; // so we give them a header
        echo "<ul>\n"; // and start an unordered list
      }
      echo "<li>" . markup_for_event($row) . "</li>\n"; // define markup_for_event yourself
    }

    Comments on this post

    • Winters agrees : I don't leave rep often but you put in so much more effort than me ;)
    • zer0efx agrees : Thanks for the detail on the post! Very informative
  6. #4
  7. The Signal:: It's everywhere!
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2007
    Location
    Sydney
    Posts
    549
    Rep Power
    497
    Wow!! Excellent info!! Thanks man! The two different queries you showed were an improvement and helped the order. Thanks! They also made sense, which is cool!

    Now you're hefty while loop, thats going to take me some time to pick through it and understand. But I see where you are going with it! Already started working it out and I def think this is something I was looking for.

    Awesome info man!! Thanks again! Will report back how I get it all working.
    Check out my Blog site if you want a good laugh, Google travelingwebguy or..
    The Official TravelingWebGuy
    Business site => New Blog Site
  8. #5
  9. The Signal:: It's everywhere!
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2007
    Location
    Sydney
    Posts
    549
    Rep Power
    497
    Alright, so I've got it almost near what I need.
    Here's how it looks currently.

    But instead of having a heading for each event, like it is now, I want to have the "Events for Managers" heading, then below it, both events for that group. I've been working on different while loops, but still can't seem to get this down.

    So my query now is:
    SQL Code:
    $ejSQL="SELECT *, ej.ejnEID as event_target_id, 
    ej.ejnAID as event_target_access, 
    FROM events as e, events_jn as ej 
    WHERE ej.ejnAID >=".$myACC." 
    AND e.eventID = ej.ejnEID 
    ORDER BY ej.ejnAID, 1";

    And my output code:
    PHP Code:
    <?php
    $last_event_type = NULL; // Keep track of what the last "person type" id is
    while ($event = mysql_fetch_assoc($ejResult)) {
    	if($event['event_target_access'] == 1) $targetAccess = 'Managers';
    	elseif($event['event_target_access'] == 2) $targetAccess = 'Supervisors';
    	else $targetAccess = 'Employees';
    	if ($last_event_type != $event['event_target_id']) {
    		$last_event_type = $event['event_target_id']; // whatever person type we're looking at now is the new old
    		echo "<p>Events for <strong>" . htmlspecialchars($targetAccess) . "</strong>"; // so we give them a header
    	}
    	?>
    	<div class="Event">
    	<div class="EventTitle"><strong><?php echo $event['eventName']; ?></strong></div> <div class="EventDate">
    	<a href="_scripts/addOutlookEvent.php">
    	<span class="tool">
    	    <img align="absmiddle" src="images/icon_calendar.png" alt="Quick Reminder" />
    	    <span class="tip"><strong>Set a Quick Reminder!</strong><br />
    	   Click on this icon to add the event to your Outlook Calendar.
    	   <p class="smText">
    	   <strong>Start:</strong> <?php $sTime = date('g:i a', UnixTime($event['eventStartTime'])); ?><?php echo $sTime; ?> <br />
    	   <strong>Finish:</strong> <?php $eTime = date('g:i a', UnixTime($event['eventEndTime'])); ?><?php echo $eTime; ?>
    	   </p></span>
    	</span>
    	</a>
    	<?php $eDate = date('M d, Y', UnixTime($event['eventDate'])); echo $eDate; ?></div>
    	<br class="clear" />
    	<?php echo substr($event['eventDesc'], 0, 150).'...<div style="float: right"><a href="event_item.php?eventID='.$event['eventID'].'" class="btnBluGlass">More Info</a></div>'; ?>
    	</div>
    	<?php echo "</p>"; ?>
    <?php } ?>

    But I can only get it to how it looks now. Not with how I wanted as explained above.

    I'm going to try out some other while loops or some foreach statements if possible.
    Check out my Blog site if you want a good laugh, Google travelingwebguy or..
    The Official TravelingWebGuy
    Business site => New Blog Site
  10. #6
  11. The Signal:: It's everywhere!
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2007
    Location
    Sydney
    Posts
    549
    Rep Power
    497
    W0000t!! I got it!

    On the last PHP code block above, I changed lines 7-9 to:
    PHP Code:
    if ($last_event_type != $targetAccess) {
    	$last_event_type = $targetAccess; // whatever person type we're looking at now is the new old
    	echo "<p>Events for <strong>" . htmlspecialchars($targetAccess) . "</strong>"; // so we give them a header
    }


    And it works perfectly!!! Thanks for the great info JT!

    Comments on this post

    • Joseph Taylor agrees : You're welcome, congratulations, and thanks for the update.
    Check out my Blog site if you want a good laugh, Google travelingwebguy or..
    The Official TravelingWebGuy
    Business site => New Blog Site
  12. #7
  13. The Signal:: It's everywhere!
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2007
    Location
    Sydney
    Posts
    549
    Rep Power
    497
    Alright, so I wanted to bring this thread back alive since it has all the useful info already posted! =)

    All the above works, and works perfectly! I added some more functionality to it using some simple Ajax BlindDown. This works too!

    The question though, is that I need help in structuring my Div layout so that I can fit all the events for the group inside 1 div container, that I can use to dropdown. Hard to comprehend from reading, so a few examples.

    So if someone has the time, can you visit:
    QU Debug Site Login: 2097
    Click on the events page and you'll see 3 headings. Managers/Supervisors/Employees. Click on the "Show / Hide" for Supervisors. There's 1 event with a border around it (for testing container size). But actually, there are 2 events for supervisors. If you view the source, look for id "Supervisors6" and "Supervisors5". The show button drops the sup6 id.

    I've been trying to find a way to structure my php so I can get all the events for a group, inside 1 div "Supervisors" so when we click to expand, it expands all the events for that group.

    So the PHP for this is: (stripped out some code that wasn't needed in the example)
    PHP Code:
    <?php 
    } else { 
    $last_event_type = NULL; // Keep track of what the last "person type" id is
    // Main While loop to go through my SQL results
    while ($event = mysql_fetch_assoc($ejResult)) {
    	if($event['event_target_access'] == 1) $targetAccess = 'Managers';
    	elseif($event['event_target_access'] == 2) $targetAccess = 'Supervisors';
    	else $targetAccess = 'Employees';
            // This is what buillds the heading for each Group
    	if ($last_event_type != $targetAccess) {
    		$last_event_type = $targetAccess; // whatever person type we're looking at now is the new old
    		echo "<p><div class='GroupHdr'>Events for " . htmlspecialchars($targetAccess) . "</div>";
    		if($myACC != $event['event_target_access']) echo '<a id="showGroup" href="javascript:Effect.Combo(\''.$targetAccess.$event['eventID'].'\');" class="smText">[ Show / Hide ]</a></div><br class="clear" />';
    	}
    ?>
    // And this is the DIV that contains the individual events, and the 
    //area I am trying to fix. I'd like to be able to put all the
    //events for the group, under this 1 ID.
    <div id="<?php echo $targetAccess.$event['eventID']; ?>" class="Event"<?php if($myACC != $event['event_target_access']) echo ' style="display: none; width: 515px; border: 1px solid #000"' ?>>
    CODE SNIPPED FROM HERE. JUST HTML FORMATTING TEXT
    </div>
    <?php echo "</p>"; ?>
    <?php } ?>

    Hope this made sense. I spent a few hours yesterday trying different while loops and I got a lot of mixed results. Been pulling out what hair I have, and I shave my head every 2 weeks!!

    So thats my new speed bump! I of course will still keep at it! Just wanted to post in case some of you see a glaring fix I dont see! Still working on my PHP Vision glasses!

    TIA!!
    Check out my Blog site if you want a good laugh, Google travelingwebguy or..
    The Official TravelingWebGuy
    Business site => New Blog Site

IMN logo majestic logo threadwatch logo seochat tools logo