I don't know how to use FOR on Wappler

Good night.
I am redesigning my site and have a data and time reservation control.

How can I check this database and load information if the time is available or the time is not available?

On my old site I loop PHP:

 <table width="100%" class="table-1">
                                <tr>
                                    <th width="29"></th>
                                    <th width="50"></th>
                                    <th width="205"></th>
                                </tr>
                                <tr class="bg-1">
                                    <td class="coll-1">&nbsp;</td>
                                    <td class="coll-1"><h6>Hora</h6></td>
                                    <td class="coll-1"><h6>Status</h6></td>
                                </tr>
                                <?php 
								$dia_da_semana = date("w");
								if ($dia_da_semana == 1)
								{
									for($x=0;$x<=23;$x++)
									{
										$hora1 = str_pad($x, 2, '0', STR_PAD_LEFT).':00';
										//$x++;
										//$hora2 = str_pad($x, 2, '0', STR_PAD_LEFT).':59';
										//$hora2 = str_pad($hora2, 2, '0', STR_PAD_LEFT);
									?>
										<tr>
											<td class="coll-1"><img src="images/usado.png" width="16" height="15"></td>
											<td class="coll-1"><?php echo $hora1 ?></td>
											<td class="coll-4">Horário Bloqueado</td>
										</tr>
									<?php
									}
								}
								else
								{
								  // lista os horarios
									for($x=0;$x<=23;$x++)
									{
										$hora1 = str_pad($x, 2, '0', STR_PAD_LEFT).':00';
										//$x++;
										//$hora2 = str_pad($x, 2, '0', STR_PAD_LEFT).':59';
										//$hora2 = str_pad($hora2, 2, '0', STR_PAD_LEFT);
										
										if ($hora1=='00:00' or $hora1=='01:00' or $hora1=='02:00' or $hora1=='03:00' or $hora1=='04:00' or $hora1=='05:00' or $hora1=='06:00' or $hora1=='07:00') 
										{
										?>
                                            <tr>
                                              <td class="coll-1"><img src="images/usado.png" width="16" height="15"></td>
                                              <td class="coll-1"><?php echo $hora1 ?></td>
                                              <td class="coll-4">Horário Bloqueado</td>
                                            </tr>
                                        <?php	
										}
										else
										{
											if(horario_inicial_disponivel_shopping(date('Y-m-d'), $hora1))
											{?>
												<tr>
													<td class="coll-1"><img src="images/livre.png" width="16" height="15"></td>
													<td class="coll-1"><?php echo $hora1 ?></td>
													<td class="coll-3">Disponivel para reserva</td>
												</tr>
										<?php 	 
											} 
											else 
											{ 
														
												$sql = 'SELECT * FROM reservas WHERE 
												data = \''.mysql_real_escape_string(date('Y-m-d')).'\' AND 
												hora_inicial = \''.mysql_real_escape_string($hora1).'\' AND
												empresa = \''.mysql_real_escape_string(1).'\'';
												$res = mysql_query($sql);
												$num = mysql_num_rows($res);
													if ($num>0)
													{
														for($i=0;$i<$num;$i++)
														{
															$status_pagamento = mysql_result($res, $i, 'status');
														}
																	
														if ($status_pagamento == 0)
														{
														?>
															<tr>
																<td class="coll-1"><img src="images/usado.png" width="16" height="15"></td>
																<td class="coll-1"><?php echo $hora1 ?></td>
																<td class="coll-5">Horário Reservado</td>
															</tr>
															<?php  } 
																	else if ($status_pagamento == 1)
																	{ ?>
															<tr>
																<td class="coll-1"><img src="images/livre.png" width="16" height="15"></td>
																<td class="coll-1"><?php echo $hora1 ?></td>
																<td class="coll-6">Horário livre</td>
															</tr>
													<?php 
														} 
													}
									  		 }	
										}
								}
								?>
                            </table>

This should help you out.

Very good !!!

I also need to set up the timetable:

for ($ x = 8; $ x <= 18; $ x ++)

from 8 to 18

http://arenaheadshot.com/site_teste/index.php fo exmple

As per your timetable example on the screenshots above, you can use a repeater with a value of your choice to repeat the table rows as many times as you wish.

So, use 16 as a repeater value, then use {{$index+8}} to start from 8, and use the pad formatter to pad the values like 08, 09 etc.
Finally add + ':00' to your expression to add the minutes in the table.

Here’s the code generated:

          <table class="table">
            <thead>
              <tr>
                <th></th>
                <th>Hours</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody is="dmx-repeat" id="repeat1" dmx-bind:repeat="16">
              <tr>
                <th></th>
                <td>{{($index + 8).pad(2) + ':00'}}</td>
                <td>Status here</td>
              </tr>
            </tbody>
          </table>
1 Like

@Teodor perfect …

and to see if a certain time has already been booked?
I need to show the customer.

would it be a loop inside loop?

Can you please explain this a little more detailed?
What and how needs to be checked?

@Teodor
when going through the loop of a database lookup if at the date
current and the time of the ($index) loop
to be able to confirm if it’s free or reserved

for exemple:

exemple the data base:

Hello @selksantos, maybe you can try using if assuming that we have strings to compare?

e.g.

<td>{{($index + 8).pad(2) + ':00'}}</td>
<td>{{hora == ($index + 8).pad(2) + ':00' ? "reserved" : "available"}}</td>

I haven’t tried it though. But the concept is if they are equal (matched) then it is reserved.

The problem is how you will query the database to buy the bank time with the loop time.

You will need to create a loop inside <td> that will loop through the results from the db query.

Another way to do this is thru several steps in Server Action. You do the matching there instead of doing it on the page. The results will give you the series of time and their statuses ready to be put inside the Bootstrap table. I usually do my calculations server-side. My page will just get the results and put them in a table.

This is what I did:
image

The date_counter are the days of the week then I query if there are hours that are “marked” for that day. Not an elegant solution, but it works. :slight_smile:

Yours will be different, I have showed this only as an example.

1 Like

I am already putting a loop on

.

I don’t know how you can get the server connection parameter (which will search the database time)

Hello @selksantos, I checked your code in your initial post and it looks like you are trying to query the database for each row that matches the time to get the status. This means you have queried the database for a list of hours for a particular date, then query the database again for the status once there is a match per row. Is my understanding correct?

Yes, that is correct.

Check my query to see if you have a time reservation.

Hi Selk, you will need the loop inside <td> not in <tr>.
I would like to suggest a different approach as I don’t know how to do the loop inside the cell that will iterate the results to match each hour. Maybe the others can help out on this.

My approach is to do the checks server-side instead (in Server Actions)

  1. Query the database using your current database query.
  2. Create a repeat step for each hour.
  3. Check if there is a match inside the repeat.
  4. Use variables to send the result to the page.

I won’t be able to reproduce it now as my laptop is currently loaded (doing a database migration).
But the concept is like this:

In your database query, the second condition will be:

tblreservas.hora equal {{var_hour}}

You may have to set the format of var_hour.
If there is no result from the database query, then it should use the initial value of var_status which is 0.

When you create the Bootstrap table, you just get the repeat1 object. The hours will be in var_hour, and the status is var_status. Just bind the cell values to these.
If var_status is 0, then the text should be “Horário livre”, if 1, then it is “Horário Reservado”.
As for the images (i.e. the status icon on your table), you can either set the image name in the steps (add var_image for example) or set the image in the page depending on the value of var_status.

Edit:
Another way to set the value of var_hour is:
image

1 Like

I tried to make the same suggestion, couldn't mount bootstrap

Can you show me where I went wrong?

UPDATED:

Hello Selk, can you please get a screenshot from the Preview tab as well? Also, what HTTP response error are you getting (500, 401, 404, etc)?

Also, your table should look something like this:

  <table class="table table-striped table-bordered table-hover table-sm">
	<thead>
	  <tr>
		<th>&nbsp;</th>
		<th>Hora</th>
		<th>Status</th>
	  </tr>
	</thead>
	<tbody is="dmx-repeat" dmx-generator="bs4table" dmx-bind:repeat="svsProcuraReservaDataAtual.data.repeat1" id="tableRepeat2">
	  <tr>
		<td></td>
		<td dmx-text="var_hour"></td>
		<td dmx-text="var_status == 1 ? &quot;Horário Reservado&quot; : &quot;Horário livre&quot;"></td>
		<td>
	  </tr>
	</tbody>
  </table>

I would like to suggest to create the table using the Bootstrap Table generator.

UPDATE:

http://arenaheadshot.com/site_teste/index.php

  1. file: "/home/arenahea/public_html/site_teste/dmxConnectLib/lib/formatters/date.php"
  2. line: 38
  3. message: "DateTime::__construct(): Failed to parse time string (HH:mm) at position 0 (H): The timezone could not be found in the database"
  4. trace: "#0 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/formatters/date.php(38): DateTime->__construct('HH:mm')↵#1 [internal function]: lib\core\formatter_dateAdd('HH:mm', 'hours', 8)↵#2 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(396): call_user_func_array('\lib\core\forma...', Array)↵#3 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(354): lib\core\Parser->objectMember(Object(Closure))↵#4 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(296): lib\core\Parser->primary()↵#5 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(273): lib\core\Parser->group()↵#6 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(253): lib\core\Parser->unary()↵#7 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(243): lib\core\Parser->multiplicative()↵#8 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(233): lib\core\Parser->addictive()↵#9 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(223): lib\core\Parser->bitwiseShift()↵#10 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(213): lib\core\Parser->relational()↵#11 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(203): lib\core\Parser->equality()↵#12 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(193): lib\core\Parser->bitwiseAnd()↵#13 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(183): lib\core\Parser->bitwiseXor()↵#14 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(173): lib\core\Parser->bitwiseOr()↵#15 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(161): lib\core\Parser->logicalAnd()↵#16 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(145): lib\core\Parser->logicalOr()↵#17 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(141): lib\core\Parser->conditional()↵#18 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(388): lib\core\Parser->expression()↵#19 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(354): lib\core\Parser->objectMember(Object(Closure))↵#20 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(296): lib\core\Parser->primary()↵#21 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(273): lib\core\Parser->group()↵#22 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(253): lib\core\Parser->unary()↵#23 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(243): lib\core\Parser->multiplicative()↵#24 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(233): lib\core\Parser->addictive()↵#25 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(223): lib\core\Parser->bitwiseShift()↵#26 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(213): lib\core\Parser->relational()↵#27 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(203): lib\core\Parser->equality()↵#28 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(193): lib\core\Parser->bitwiseAnd()↵#29 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(183): lib\core\Parser->bitwiseXor()↵#30 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(173): lib\core\Parser->bitwiseOr()↵#31 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(161): lib\core\Parser->logicalAnd()↵#32 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(145): lib\core\Parser->logicalOr()↵#33 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(141): lib\core\Parser->conditional()↵#34 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/core/Parser.php(86): lib\core\Parser->expression()↵#35 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(206): lib\core\Parser->parse('NOW.formatDate(...', NULL)↵#36 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(191): lib\App->parseObject('{{NOW.formatDat...', NULL)↵#37 /home/arenahea/public_html/site_teste/dmxConnectLib/modules/core.php(91): lib\App->parseObject(Object(stdClass))↵#38 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(163): modules\core->setvalue(Object(stdClass), 'var_hour')↵#39 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(128): lib\App->execSteps(Object(stdClass))↵#40 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(98): lib\App->execSteps(Array)↵#41 /home/arenahea/public_html/site_teste/dmxConnectLib/modules/core.php(62): lib\App->exec(Object(stdClass), true)↵#42 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(163): modules\core->repeat(Object(stdClass), 'repeat1')↵#43 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(128): lib\App->execSteps(Object(stdClass))↵#44 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(98): lib\App->execSteps(Array)↵#45 /home/arenahea/public_html/site_teste/dmxConnectLib/lib/App.php(71): lib\App->exec(Object(stdClass))↵#46 /home/arenahea/public_html/site_teste/dmxConnect/api/GLOBAL/HOME/VERIFICA_RESERVA.php(8): lib\App->define(Object(stdClass))↵#47 {main}"

Hi Selk, so your hours are varchar.

Can you please try similar to the following:

In your database query, change the second condition to:

tblreservas.hora equal {{test_hour}}

Then try to Open in browser to test it out for the meantime and check the result and the Preview Tab?
image