Jump to content

Recommended Posts

Hi,

 

I was thinking of creating a table for this current code and place only 3 options in a row, however I do not know how to execute a command which will allow me to do so, I have attached 2 pictures to describe what I will like to achieve. Below is my code as well. Any advice? Thanks

 

 

$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");

$sql = mysqli_query($dbc, $query)

or die(mysql_error());

while($data = mysqli_fetch_array($sql))

{

echo'<table>';

echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';

echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>';

echo'<table>';

}

$count = 0;
$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");
      $sql = mysqli_query($dbc, $query)
      or die(mysql_error());
      while($data = mysqli_fetch_array($sql))
      {
[$count++;
         echo'<table>';
            echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';
            echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>';
         echo'<table>';
         if ($count==3) {
              echo '<br>';
              $count=0;
         }
      }

Hi Spleshmen,

 

I would like to do it in table format, meaning to say I will also need to use <tr> and <td>, in columns of 3. Where can I add <tr> and <td>? Thanks

 

$count++;

        echo'<table>';

            echo '<input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';

            echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label>';

        echo'<table>';

        if ($count==3) {

              echo '<br>';

              $count=0;

Hi,

 

I did this codes, but it did not turn out to be what I wanted, I have attached the picture of rows which I would like to achieve. Any suggestion

 

<?php

$dbc = mysqli_connect('localhost', '***', '***', '***')

or die(mysqli_error());

$count = 0;

$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");

$sql = mysqli_query($dbc, $query)

or die(mysql_error());

while($data = mysqli_fetch_array($sql))

{

$count++;

echo'<table>';

echo '<tr>';

echo '<td><input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';

echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>';

echo '<tr>';

echo'<table>';

if ($count==3) {

echo '<br />';

$count = 0;

}

}

?>

 

 

The HTML code turns out to be this...

 

<table><tr><td><input name="district" type="checkbox" id="1" value="1"><label for="1">North</label></td>

 

<tr><table><table><tr><td><input name="district" type="checkbox" id="2" value="2"><label for="2">North West</label></td>

 

[attachment deleted by admin]

Hi,

 

I have rectified my codes, but it still doesnt work. Any suggestions?

 

 

<?php

$dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase')

or die(mysqli_error());

$count = 0;

$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");

$sql = mysqli_query($dbc, $query)

or die(mysqli_error());

while($data = mysqli_fetch_array($sql))

{

echo'<table>';

$count++;

echo '<tr><td><input name="district" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';

echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>';

echo '</tr>';

if ($count==3) {

echo '</tr></table><br />';

$count = 0;

}

}

?>

Since it's always the same, just construct it all within the while loop. There's no need for a counter checking how many elements have been retrieved since you already know how many there will be.

Hi,

 

Currently, I have an issue with the alignment, attached is the picture as an example.

 

In the picture, the one circled in red is what I want to achieve (rows of 3), however come to the 2nd line, which is circled in blue, it became (rows of 4) and misaligned, below is my coding.

 

Can someone tell me where it went wrong? Thanks

 

 

<?php

$dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase')

or die(mysqli_error());

$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");

$sql = mysqli_query($dbc, $query)

or die(mysqli_error());

echo'<table><tr>';

while($data = mysqli_fetch_array($sql))

{

$count++;

echo '<td><input name="district[]" type="checkbox" id="'.$data['district_id'].'" value="'.$data['district_id'].'">';

echo '<label for="'.$data['district_id'].'">'.$data['district_name'].'</label></td>';

if ($count==3) {

            echo '</tr></table>';

            $count = 0;

}

 

}

?>

 

[attachment deleted by admin]

This is a common problem for PHP newcomers; x records per x columns.  It is in this situation that you make use of PHP's modulus operator

 

It is this operator that allows you to divide a number by X, but only get the remainder of it.  Combine this with a counter to three and you have your solution.

 

For example, using your current code... modified a bit.

      $dbc = mysqli_connect('localhost', 'aliendatabase', '1234567', 'aliendatabase')
      or die(mysqli_error());
      $query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC");
      $sql = mysqli_query($dbc, $query)
      or die(mysqli_error());
      echo'</pre>
<table>'; // Start your table outside the loop... and your first row
      $count = 0; // Start your counter
      while($data = mysqli_fetch_array($sql)) {
      /* Check to see whether or not this is a *new* row
          If it is, then end the previous and start the next and restart the counter.
      */
            if ($count % 3 == 0) { echo ""; $count = 0;}
            echo '';
            echo ''.$data['district_name'].'';
            $count++ //Increment the count
      }
      echo "</table>"; //Close your last row and your table, outside the loop<br>?&g

Hi Zanus,

 

It works!!! Finally! Thanks

 

I have one last question. Currently, ID 1 - 15 in my database belongs to 'North' zone. How do I specify in my command, if I'm not wrong, it is got to do with 'LIMIT' command?

 

$query = ("SELECT * FROM tutor_preferred_district ORDER BY district_id ASC"); --> what should I add in here? Thanks

 

 

Thanks.

 

But beside 'North' zone, I have 'West', 'East' and 'South'

 

North starts from 1 - 15 West 16 - 25 etc.

 

So how do I perform the right coding for North and West?

 

 

SELECT * FROM tutor_preferred_district ORDER BY district_id ASC LIMIT 1 TO 15?

 

SELECT * FROM tutor_preferred_district ORDER BY district_id ASC LIMIT 16 TO 25?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.