Page 1 of 1

Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 4:06 am
by cogito
Hi !

I am trying to create a booking system in php and mysql with a datepicker.
When user selects a date a table with available period times should be displayed. If a time frame is available, the user should use the radio input and book that perioad of time.
If I'm using
"ORDER BY start ASC LIMIT 1"
it gives me 8:22 exists, and it's correct, but it not gives me the second statement, 8:30 exists. Instead, for 8.30 it creates available time frame 8:30-8:38.
If I'm using
"ORDER BY start ASC"
it multiplies the information based on how many records I have in database.
I can't see what is wrong !
Bellow are my code for mysql table:

Code: Select all

    CREATE TABLE IF NOT EXISTS `bookings` (
      `id` int(100) NOT NULL AUTO_INCREMENT,
      `date` date NOT NULL,
      `start` enum('8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54','8:54-9:02','9:02-9:10','9:18-9:26','9:26-9:34','9:34-9:42','9:42-9:50','9:50-10:58','10:58-11:06','11:06-11:14','11:14-11:22','11:22-11:30','11:30-11:38','11:38-11:46','11:46-11:54','11:54-12:02','12:30-12:38','12:38-12:46','12:46-12:54','12:54-13:02','13:02-13:10','13:10-13:18','13:18-13:26','13:26-13:34','13:34-13:42','13:42-13:50','13:50-13:58','13:58-14:06','14:06-14:14','14:14-14:22','14:22-14:30','14:30-14:38','14:38-14:46','14:46-14:54','14:54-15:02','15:02-15:10','15:10-15:18','15:18-15:26','15:26-15:34') COLLATE utf8_unicode_ci NOT NULL,
      `booked` enum('0','1') COLLATE utf8_unicode_ci NOT NULL,
      `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
      `email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
      `phone` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `start` (`start`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

    --
    -- Dumping data for table `bookings`
    --

    INSERT INTO `bookings` (`id`, `date`, `start`, `booked`, `name`, `email`, `phone`) VALUES
    (2, '2016-05-06', '8:30-8:38', '1', 'vcv', 'vxcv', 'xcvvc'),
    (1, '2016-05-06', '8:22-8:30', '1', 'zxccxz', 'xczcc', 'zxczxc'),
    (4, '2016-05-09', '9:02-9:10', '1', '', '', ''),
    (3, '2016-05-09', '8:38-8:46', '1', '', '', '');
And the code for index.php is:

Code: Select all

        <?php
	$conn = mysqli_connect("localhost", "", "", "");
	
	$post_at = "";
	$post_at_to_date = "";
	
	$queryCondition = "";
	if(!empty($_POST["search"]["post_at"])) {			
		$post_at = $_POST["search"]["post_at"];
		list($fid,$fim,$fiy) = explode("-",$post_at);
		
		$post_at_todate = date('Y-m-d');
		if(!empty($_POST["search"]["post_at"])) {
			$post_at_to_date = $_POST["search"]["post_at"];
			list($tid,$tim,$tiy) = explode("-",$_POST["search"]["post_at"]);
			$post_at_todate = "$tiy-$tim-$tid";
		}
		
		$queryCondition .= "WHERE date = '" . $post_at_todate . "' ";
	}

	$sql = "SELECT * from bookings " . $queryCondition . " ORDER BY start ASC LIMIT 1";
	$result = mysqli_query($conn,$sql);
	
    ?>

    <html>
	<head>
    <title>Recent Articles</title>		
	<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
	<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

	<style>
	.table-content{border-top:#CCCCCC 4px solid; width:50%;}
	.table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;} 
	.table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;} 
	</style>
	</head>
	
	<body><input type="text" id="field_results" name="field_results" />  ...
    <div class="demo-content">
		<h2 class="title_with_link">Recent Articles</h2>
      <form name="frmSearch" method="post" action="">
	 <p class="search_input">
		    <input type="text" placeholder="From Date" id="post_at" name="search[post_at]" value="<?php echo $post_at; ?>" class="input-control" />			 
		<input type="submit" name="go" value="Search" >
	</p>
    <?php if(!empty($result))	 { ?>
    <table class="table-content">
          <thead>
        <tr>
                  
          <th width="30%"><span>Day</span></th>
          <th width="50%"><span>Time period</span></th>           
          <th width="20%"><span>Book</span></th>	  
        </tr>
      </thead>
    <tbody>
	<?php

		//while ($row=mysqli_fetch_row($result))
		while($row = mysqli_fetch_array($result)) {
			if(preg_match('/8:22-8:30/',$row['start'])==TRUE){
				echo "8:22 exists";
			}else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:22-8:30</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:22-8:30' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/8:30-8:38/',$row['start'])==TRUE){
				echo "8:30 exists";
			}else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:30-8:38</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:30-8:38' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/8:38-8:46/',$row['start'])){
				echo "8:38 exists";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:38-8:46</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:38-8:46' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/8:46-8:54/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:46-8:54</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:46-8:54' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/8:54-9:02/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:54-9:02</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:54-9:02' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:02-9:10/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:02-9:10</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:02-9:10' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:18-9:26/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:18-9:26</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:18-9:26' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:26-9:34/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:26-9:34</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:26-9:34' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:34-9:42/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:34-9:42</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:34-9:42' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:42-9:50/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:42-9:50</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:42-9:50' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/9:50-10:58/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>9:50-10:58</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:50-10:58' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/10:58-11:06/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>10:58-11:06</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '10:58-11:06' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:06-11:14/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:06-11:14</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:06-11:14' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:14-11:22/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:14-11:22</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:14-11:22' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:22-11:30/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:22-11:30</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:22-11:30' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:30-11:38/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:30-11:38</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:30-11:38' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:38-11:46/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:38-11:46</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:38-11:46' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:46-11:54/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:46-11:54</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:46-11:54' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/11:54-12:02/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>11:54-12:02</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:54-12:02' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/12:30-12:38/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>12:30-12:38</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:30-12:38' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/12:38-12:46/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>12:38-12:46</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:38-12:46' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/12:46-12:54/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>12:46-12:54</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:46-12:54' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/12:54-13:02/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>12:54-13:02</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:54-13:02' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:02-13:10/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:02-13:10</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:02-13:10' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:10-13:18/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:10-13:18</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:10-13:18' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:18-13:26/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:18-13:26</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:18-13:26' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:26-13:34/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:26-13:34</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:26-13:34' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:34-13:42/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:34-13:42</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:34-13:42' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:42-13:50/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:42-13:50</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:42-13:50' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:50-13:58/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:50-13:58</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:50-13:58' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/13:58-14:06/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>13:58-14:06</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:58-14:06' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:06-14:14/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:06-14:14</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:06-14:14' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:14-14:22/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:14-14:22</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:14-14:22' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:22-14:30/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:22-14:30</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:22-14:30' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:30-14:38/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:30-14:38</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:30-14:38' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:38-14:46/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:38-14:46</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:38-14:46' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:46-14:54/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:46-14:54</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:46-14:54' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/14:54-15:02/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>14:54-15:02</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:54-15:02' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/15:02-15:10/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>15:02-15:10</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:54-15:02' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/15:10-15:18/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>15:10-15:18</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:10-15:18' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/15:18-15:26/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>15:18-15:26</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:18-15:26' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/15:26-15:34/',$row['start'])){
				echo "";
			} else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>15:26-15:34</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:26-15:34' .'"></td>';
				echo "</tr>";
			}
	 ?>
        <tr>
			

		</tr>
    <?php
		}
    ?>
    <tbody>
    </table>
    <?php } ?>
    </form>
    </div>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
    $.datepicker.setDefaults({
    showOn: "button",
    buttonImage: "datepicker.png",
    buttonText: "Date Picker",
    buttonImageOnly: true,
    dateFormat: 'dd-mm-yy'  
    });
    $(function() {
    $("#post_at").datepicker();
    $("#post_at_to_date").datepicker();
    });
    </script>
    <script>
    $(document).ready(function () {
    $(":radio").on('click', function () {

        var fields = '';
        $(":radio").each(function () {
            if (this.checked) {
                fields += $(this).val() + ' ';
            }
        });
        $('#field_results').val($.trim(fields))

    });
    });
    </script>
    </body></html>

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 6:41 am
by Celauran
You're creating all the rows on every iteration. When you add LIMIT 1, the first exists is displayed because that's the first iteration. The remaining are displayed beneath it. The second slot is not marked as 'exists' because you've not yet gotten to that iteration and, with LIMIT 1, you won't. An alternate approach you may want to consider is to fetch all existing bookings, create the booking table separately, and check if any given row exists in the result set before printing it.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 6:51 am
by cogito
Thank you !
Maybe I get to that iteration using switch statement ?
What method do you think it's better ?

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 6:55 am
by Celauran
I think you want to separate out the fetching of the existing bookings from the table generation. There's a lot of duplication in the table generation that can also be removed. Pull in existing bookings and shove them in an array. Create the table and, when generating each table row, check if that time slot exists in the array to determine whether to display booking available or already booked.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 7:04 am
by cogito
Where are the duplicates? What is the code you are thinking off.
Right now, the wheels in my head refuse to work after 2 hours of trying in vain.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 7:07 am
by Celauran
cogito wrote:Where are the duplicates?

Code: Select all

 if(preg_match('/8:22-8:30/',$row['start'])==TRUE){
                                echo "8:22 exists";
                        }else {
                                echo "<tr>";
                                echo "<td>$post_at</td>";
                                echo "<td>8:22-8:30</td>";
                                echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:22-8:30' .'"></td>';
                                echo "</tr>";
                        }
                        if(preg_match('/8:30-8:38/',$row['start'])==TRUE){
                                echo "8:30 exists";
                        }else {
                                echo "<tr>";
                                echo "<td>$post_at</td>";
                                echo "<td>8:30-8:38</td>";
                                echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:30-8:38' .'"></td>';
                                echo "</tr>";
                        }
                        if(preg_match('/8:38-8:46/',$row['start'])){
                                echo "8:38 exists";
                        } else {
                                echo "<tr>";
                                echo "<td>$post_at</td>";
                                echo "<td>8:38-8:46</td>";
                                echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:38-8:46' .'"></td>';
                                echo "</tr>";
                        }
etc. That can all be reduced to a single block.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 7:10 am
by Celauran
Having that ID duplicated everywhere is also potentially problematic. IDs are meant to be unique and you'll run into problems with JS selectors with duplicates.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 7:17 am
by cogito
Yes. You are right !
How will you condense the code for the first 2 statements? If 8:22-8:30 and 8:22-8:30 exists and 8:30-8:38 and 8:38-8:46 does not, how will you echo radios only for 8:30-8:38 and 8:38-8:46 ?

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 7:49 am
by Celauran
In your SQL table definition, you have the following:

Code: Select all

enum('8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54','8:54-9:02','9:02-9:10','9:18-9:26','9:26-9:34','9:34-9:42','9:42-9:50','9:50-10:58','10:58-11:06','11:06-11:14','11:14-11:22','11:22-11:30','11:30-11:38','11:38-11:46','11:46-11:54','11:54-12:02','12:30-12:38','12:38-12:46','12:46-12:54','12:54-13:02','13:02-13:10','13:10-13:18','13:18-13:26','13:26-13:34','13:34-13:42','13:42-13:50','13:50-13:58','13:58-14:06','14:06-14:14','14:14-14:22','14:22-14:30','14:30-14:38','14:38-14:46','14:46-14:54','14:54-15:02','15:02-15:10','15:10-15:18','15:18-15:26','15:26-15:34') 
If you had this represented in your code, you could easily iterate over it to construct your bookings table. At the same time, you could check the existence of a given block in your result set.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 8:41 am
by cogito
I don't want to use a select menu but radio for every single time frame.
Or you are thinking of :

Code: Select all

$time_frame = "'8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54'";
And here you lost me!!!

How do I compare $time_frame with returned $row['start'] ?

Am I still using the code bellow? If so, is not ok because it outputs the radio as if in the database there is no entry. :banghead:

Code: Select all

while($row = mysqli_fetch_array($result)) {
			if(preg_match('/$time_frame/',$row['start'])==TRUE){
				echo "8:22 exists";
			}else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:22-8:30</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:22-8:30' .'"></td>';
				echo "</tr>";
			}
			if(preg_match('/$time_frame/',$row['start'])==TRUE){
				echo "8:30 exists";
			}else {
				echo "<tr>";
				echo "<td>$post_at</td>";
				echo "<td>8:30-8:38</td>";
				echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:30-8:38' .'"></td>';
				echo "</tr>";
			}

Re: Something wrong with mysqli_fetch_array in php?

Posted: Mon May 09, 2016 8:50 am
by Celauran
cogito wrote:I don't want to use a select menu but radio for every single time frame.
Sure. I never suggested anything about a select list.
cogito wrote:Or you are thinking of :

Code: Select all

$time_frame = "'8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54'";
An array might be easier.
cogito wrote:How do I compare $time_frame with returned $row['start'] ?
in_array?
cogito wrote:Am I still using the code bellow?
Yes, but separate it from the fetching of your result set. If you've got an array of all possible time frames, you can iterate over that array to build up your table. On each pass, you check if the current slot is in the already booked array.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Tue May 10, 2016 7:09 am
by cogito
Celauran please enlighten me !

I've tried all the variants, the last one I've tested is this:

Code: Select all

$queryCondition .= "WHERE date = '" . $post_at_todate . "' AND booked='1'";

	$sql = "SELECT * from bookings " . $queryCondition . " ORDER BY start ASC";
	$result = mysqli_query($conn,$sql);

$time_frame = array('8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54','8:54-9:02','9:02-9:10','9:18-9:26','9:26-9:34','9:34-9:42','9:42-9:50','9:50-10:58','10:58-11:06','11:06-11:14','11:14-11:22','11:22-11:30','11:30-11:38','11:38-11:46','11:46-11:54','11:54-12:02','12:30-12:38','12:38-12:46','12:46-12:54','12:54-13:02','13:02-13:10','13:10-13:18','13:18-13:26','13:26-13:34','13:34-13:42','13:42-13:50','13:50-13:58','13:58-14:06','14:06-14:14','14:14-14:22','14:22-14:30','14:30-14:38','14:38-14:46','14:46-14:54','14:54-15:02','15:02-15:10','15:10-15:18','15:18-15:26','15:26-15:34');


while($row = mysqli_fetch_array($result)){
    echo '<tr>';

    if($row['start'] !== $time_frame){
        foreach($row as $each){
			echo '<td><input class="text" type="radio" id="rez1" name="rez" value="'.$each.'"></td>';
        }
    }
    echo '</tr>';
  }

echo '</table></div>';
but is not OK.

Please show me the code you are thinking of.

Re: Something wrong with mysqli_fetch_array in php?

Posted: Tue May 10, 2016 8:18 am
by Celauran
You're still approaching this backwards. Use the $time_frame to build your table, not $result. Try something like this:

Code: Select all

<?php

// These can be retrieved from the DB so changes don't need to be made in two places
$time_frame = array('8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54','8:54-9:02','9:02-9:10','9:18-9:26','9:26-9:34','9:34-9:42','9:42-9:50','9:50-10:58','10:58-11:06','11:06-11:14','11:14-11:22','11:22-11:30','11:30-11:38','11:38-11:46','11:46-11:54','11:54-12:02','12:30-12:38','12:38-12:46','12:46-12:54','12:54-13:02','13:02-13:10','13:10-13:18','13:18-13:26','13:26-13:34','13:34-13:42','13:42-13:50','13:50-13:58','13:58-14:06','14:06-14:14','14:14-14:22','14:22-14:30','14:30-14:38','14:38-14:46','14:46-14:54','14:54-15:02','15:02-15:10','15:10-15:18','15:18-15:26','15:26-15:34');

// This is vulnerable to SQL injection. Look at prepared statements
$queryCondition .= "WHERE date = '" . $post_at_todate . "' AND booked='1'";

$sql = "SELECT * from bookings " . $queryCondition . " ORDER BY start ASC";
$result = mysqli_query($conn,$sql);

/**
 * We don't want to create the table by iterating over the results. That won't work.
 * The table needs to display all slots, available or not, so we must use $time_frame
 * to build the table. In order to check if any given slot is already booked, we
 * can store ALL results in an array, indexed by time slot, and use array_key_exists
 * to check if a slot is available or booked
 */
$bookings = [];
while ($row = mysqli_fetch_assoc($result)) {
    $bookings[$row['start']] = $row;
}

// If we're only interested in 'start', there's really no need to SELECT *

foreach ($time_frame as $slot) {
    echo '<tr><td>'; // You really shouldn't be echoing HTML. Separate logic and templating.

    // Now we check if the current slot is booked
    if (array_key_exists($slot, $bookings)) {
        echo 'Booked';
    } else {
        echo '<input class="text" type="radio" name="rez" value="' . $slot . '">';
    }
    echo '</td></tr>';
}

Re: Something wrong with mysqli_fetch_array in php?

Posted: Tue May 10, 2016 10:20 am
by cogito
God bless you ! You are a great person !