How to create a for loop in chronoforms

Tgoodrich 18 May, 2014
Hi,

I am trying to list out several member contacts from a database and I need a for loop to get them all.

I need the for loop to loop through the data if it meets 2 conditions which would be "member_id" and "seat_held". I also have model ID of "Contact".

How would I write this out in chronoform language? In other words what would my for loop look like given the conditions above?

Thanks
Tgoodrich 18 May, 2014
This is the code I'm working with:

<div id="nteam">
	<div class="container">
		<?php
		for($i=1; $form->data[Contact]['id'.$i] != NULL ; $i++)
		{

$image = $form->data[Contact]["uploaded_image"];
$image1 = ("no-image.jpg");
if($image != "")
			$image = 'components/com_chronoforms5/chronoforms/uploads/contact_information/'.basename($image);
		else
			$image = 'components/com_chronoforms5/chronoforms/uploads/contact_information/'.basename($image1);
$member_id = $form->data[Contact]["member_id"];
$name = $form->data[Contact]["member_name"];
if($name != "")
			$name = ($name);
		else
			$name = ("Unavailable");
$email = $form->data[Contact]["email_address"];
if($email != "")
			$email = ($email);
		else
			$email = ("Unavailable");
$phone = $form->data[Contact]["member_phone"];
if($phone != "")
			$phone = ($phone);
		else
			$phone = ("Unavailable");
$position = $form->data[Contact]["other_info"];
if($position != "")
			$position = ($position);
		else
			$position = ("Unavailable");


		?>
			<div class="member">
				<div class="details">
					<?php 
					if($link != "")
					{
					?>
					<a data-rokbox data-rokbox-size="450 675" href="/~townofsa/index.php/component/chronoforms5/?chronoform=elected-officials-contact-form&<?php echo $member_1d;?>&tmpl=component" target="_blank">
					<img alt="<?php echo $name;?>" src="<?php echo $image;?>" height="170px"/>
					</a>
					<?php
					}
					else
					{
						echo '<img alt="'.$name.'" src="'.$image.'" height="170px"/>';
					}
					if($link != "")
					{
					?>
						<a data-rokbox data-rokbox-size="450 675" href="/~townofsa/index.php/component/chronoforms5/?chronoform=elected-officials-contact-form&<?php echo $member_1d;?>&tmpl=component" target="_blank">
							<h2 class="title"><?php echo $name;?></h2>
						</a>
					<?php
					}
					else 
					{
						echo '<h2 class="title">'.$name.'</h2>';
					}
					?>
					<?php 
					if($position != "")
					{
					?>
						<h5 class="field1"><?php echo $position;?></h5>
					<?php
					}
					if($field2!= "")
					{
					?>
						<h5 class="field2"><?php echo $field2;?></h5>
					<?php 
					} 
					?>
				</div>
			</div>
			<?php
			if($imageno!=0 && $i%$imageno == 0) 
			{?>
			<div style="clear:both;"></div>
			<?php }
		}?>
	</div>
</div>
<div style="clear:both;"></div>


Its from a joomla module that I'm trying to recreate using chronoforms.

So far my output can be seen here:
http://184.154.228.17/~townofsa/index.php?option=com_chronoforms5&chronoform=test1-contact

I want the end result to look like this:
http://184.154.228.17/~townofsa/index.php/contact-us/town-government

The code above should work if i get the for loop syntax right which as of now I now its not.

Also, I want to use the code above in a slider. For instance I will place the code above in a "custom" box which will be inside a slider. In that slider I want it to get and list out all of our towns select board members. This would be for slider 1.

In the same slider area I will have another slider (slider 2) which will again have the code above inside a "custom" box accept this time it will list out all the town clerk employees.

When done I will have about 10 sliders inside 1 slider area. Each slider will be for a different group of people, e.g:select board members, town clerk employees, highway department employees, drb officers etc..

I will have a table that contains all the records for each employee.

I dont really know what I'm doing but it seems this should work as long as I can get the loop right.

thanks
Max_admin 19 May, 2014
Answer
1 Likes
Hi Tgoodrich,

You need a foreach loop, a code example in your case would be like this:

<?php
foreach($form->data[Contact] as $contact){
//do processing and any output here, you can close the php tags if needed then open them again
}
?>


This is straight forward, you just need to control the output inside the loop!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Tgoodrich 24 May, 2014
Hi Max,

I tried your code and it works perfectly.

Heres the code i used:
<div id="nteam">
	<div class="container">
	<div class="contact-header">To submit a contact form and view contact information for the Town Agent, please click on their photo below.</div>
        <?php

foreach($form->data[Contact] as $contact)
	{
	if ($contact[office_id] == 'town-agent')
		{
?>
	       <div class="member">
		       <div class="details">
					<?php
		if ($contact[uploaded_image] != "")
			{
?>
					    <a data-rokbox data-rokbox-size="450 675" href="/~townofsa/index.php/component/chronoforms5/?chronoform=elected-officials-contact-form&office=<?php
			echo $contact[office_id]; ?>&id=<?php
			echo $contact[member_id]; ?>&tmpl=component" target="_blank"><img alt="<?php
			echo $contact[member_name]; ?>" src="components/com_chronoforms5/chronoforms/uploads/contact_information/<?php
			echo $contact[uploaded_image]; ?>" height="170px"/></a>
					<?php
			}
		  else
			{
			echo '<a data-rokbox data-rokbox-size="450 675" href="/~townofsa/index.php/component/chronoforms5/?chronoform=elected-officials-contact-form&office=' . $contact[office_id] . '&id=' . $contact[member_id] . '&tmpl=component" target="_blank"><img alt="' . $contact[member_name] . '" src="components/com_chronoforms5/chronoforms/uploads/contact_information/no-image.jpg" height="170px"/></a>';
			}

		if ($contact[uploaded_image] != "")
			{
?>
						<a data-rokbox data-rokbox-size="450 675" href="/~townofsa/index.php/component/chronoforms5/?chronoform=elected-officials-contact-form&office=<?php
			echo $contact[office_id]; ?>&id=<?php
			echo $contact[member_id]; ?>&tmpl=component" target="_blank"><h2 class="title"><?php
			echo $contact[member_name]; ?></h2></a>
					<?php
			}
		  else
			{
			echo '<h2 class="title">' . $contact[member_name] . '</h2>';
			}

?>
					<?php
		if ($contact[office_id] == 'town-agent')
			{
?>
						<h5 class="field1"><?php
			echo $contact[internal_position]; ?></h5>
					<?php
			} ?>
		       </div>
	      </div>
	      <?php
		} ?>
	    <?php
	} ?>
    </div>
</div>

Max or Bob, this code works but could you take a look at it and let me know if the syntax looks correct or if there would be a better way to write the code?

I have 11 sliders and had to put this code into each slider. Each has only a slight variation, i.e I had to change the "office_id" and contact header.

I still have to get some images uploaded and do some more css work but heres the final output using the code above. All completely made using chronoforms.
http://184.154.228.17/~townofsa/index.php/contact-us/town-government

Thanks
GreyHead 25 May, 2014
1 Likes
Hi Tracey,

Here's a tightened up version of your code which removes some of the repetitions
<div id="nteam">
  <div class="container">
    <div class="contact-header">To submit a contact form and view contact information for the Town Agent, please click on their photo below.</div>
<?php
$image_url = JURI::root().'components/com_chronoforms5/chronoforms/uploads/contact_information/';
$url_root  = JURI::root().'index.php?option=com_chronoforms5&chronoform=elected-officials-contact-form&tmpl=component&';
foreach($form->data['Contact'] as $contact) {
  if ( $contact['office_id'] == 'town-agent' ) {
    $url = "{$url_root}office={$contact['office_id']}&id={$contact['member_id']}";
    $a_start = "<a data-rokbox data-rokbox-size='450 675' href='{$url}' target='_blank'>";
    $h2 = "<h2 class='title'>{$contact['member_name']}</h2>";

    if ( $contact['uploaded_image'] != "" ) {
      $image = $contact['uploaded_image'];
      $h2 = $a_start.$h2.'</a>';
    } else {
      $image = 'no-image.jpg';
    }
    echo "
      <div class='member' >
        <div class='details'>
          {$a_start}
            <img alt='{$contact['member_name']}' src='{$image_url}{$image}' height='170px'/>
          </a>
          {$h2}
          <h5 class='field1'>{$contact['internal_position']}</h5>
      </div>
    </div>";
  }
}
?>
  </div>
</div>
Not tested - check on quotes and brackets!

Bob
Tgoodrich 25 May, 2014
Hi Bob,

Thanks so much for looking the code over and taking the time to rewrite it. In my editor my code is 59 lines and you were able to knock it down to 33 lines. Thats just shy of half my code! WoW!

I tested your code as is and it works perfectly. It amazes me how there can be several different ways to accomplish the same thing. I'm going to replace all my code with yours, as it's going to be much easier to work with.

As a newbie with no php or database schooling (all my schooling is in the automotive industry) I am really finding all this very interesting and can't wait to learn more and be able to do more with php and ChronoForms. The rewritten code gives me a lot to learn from and will help alot in future projects.

Kind regards,
Tracey (lol, not Tim)
GreyHead 26 May, 2014
1 Likes
Hi Tracey,

Apologies for getting your name wrong :-(

Some things to look at.

a) if you are going to do coding then it is worth finding a code editor that will highlight your test and, if possible, 'lint' it to flag up possible errors. There are many around, I currently use Sublime Text 3 with the SublimeLinter plug-in (and a bunch of others too).

b) The {$variable_name} syntax is really useful as it can reduce the use of <?php echo $variable_name; ?> and makes the code more readable. Note that it only works inside double quotes e.g. echo ". . ."; and only with variable names starting with $

c) simplify the logic and move everything that doesn't change outside the if . . . else . . . construct.

d) put long repeated strings into variables so that you only have one place to edit them.

There's more but those four cover most of my edits to your code.

Bob
Tgoodrich 26 May, 2014
Hi Bob,

Thanks for letting me know about Sublime, I've downloaded and installed it but haven't tried it out yet. Looks like it will be very helpful though. I have been using my old Macromedia Dreamweaver 8 because it will highlight the code however lets just say its a bit behind the times now considering Macromedia was bought out by Adobe years ago.

Also thanks for all the good advice, I will definitely put it to use the next time around.

Lots to learn!

Kind Regards,
Tracey
This topic is locked and no more replies can be posted.