HTML Form: Menu list

This entry is part 10 of 10 in the series HTML Form

For situations where you have a long list of options, consider using menu or list.

A list can contain as many options as needed, yet it takes up very little space on the form.

To create a list, start with a two-sided <select> tag.  Within it, place each option in its own <option> tag.

<!DOCTYPE html>
<html>
<head lang='en'>
    <meta charset='UTF-8'>
    <title>Codecrawl.com-Form</title>

</head>
<body>

<p>Where would you like to go next?</p>

<form method='post'>
    <select name='country'>
        <option value='england'>England</option>
        <option value='brazil'>Brazil</option>
        <option value='malaysia'>Malaysia</option>
    </select>

</form>

</body>
</html>

 

jQuery Mobile: Basic drop down or select menu

This entry is part 66 of 73 in the series jQuery Mobile

This is the familiar drop down or select menu.  

This is the so called native drop down menu as constructing it is similar to constructing a web page drop down menu using Javascript.

<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.js'></script>
    <title>Upwhere</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header' data-theme='b'>
        <h1>Welcome To My Drop Down Menu App</h1>
    </div>

    <div data-role='content'>
        <form>
            <div class='ui-field-contain'>
                <select name='select-native-1' id='select-native-1'>
                    <option value='1'>The 1st Option</option>
                    <option value='2'>The 2nd Option</option>
                    <option value='3'>The 3rd Option</option>
                    <option value='4'>The 4th Option</option>
                </select>
            </div>
        </form>
    </div>

    <div data-role='footer' data-position='fixed'>
        <h4 style='text-align:center;'>I'm the footer</h4>
    </div>
</div>

</body>
</html>
Try The Code

PHP MySQL: Select a single record using fetch_assoc

This entry is part 14 of 17 in the series PHP & MySQL

A single record in the table can be retrieved by using fetch_assoc().

We have this table as below and let’s say we ask ourselves when was Nirvana’s Smell Like Teen Spirit released.

IDartistealbumyear
1Rod StewartMaggie May1973
2New OrderSubstance1987
3NirvanaSmell Like Teen Spirit1992
4Rod StewartOut of Order1988
Run the SQL

We first have to make some adjustments to the query statement:

select year from artist where artiste='Nirvana' and album='Smell Like Teen Spirit'
Run the SQL
<?php
$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
    exit();
}

$query = "select year as yr from artist where artiste='Nirvana' and album='Smell Like Teen Spirit'";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("Smell Like Teen Spirit was released in: %d", $row["yr"]);
        echo "<br>";
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
?>

We have used associative array method fetch_assoc().

If the above script is ran, the output in the browser is as below:

select4

PHP MySQL: Select Count Using fetch_assoc()

This entry is part 10 of 17 in the series PHP & MySQL

If we are to use

select count(*) from artist

in the query statement, we can use the fetch_assoc() method provided by PHP.

The syntax is:

array mysqli_result::fetch_assoc ( void )

select count(*) from artist

will only return a row and a column.  For associative array, we would need to have a column name.

We can assign a column name cnt as

select count(*) as cnt from artist

The PHP code would be as below:

<?php
$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
    exit();
}

$query = "select count(*) as cnt from artist ";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */

    $row = $result->fetch_assoc();
    printf("Number of records:  %s", $row["cnt"]);


    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
?>

Here the screenshot after running the script above:

select2

PHP MySQL: Select count

This entry is part 9 of 17 in the series PHP & MySQL

When it comes to querying a database, we always like to use select count to get the number of records in the table.

select count(*) from artist

However, let’s look at the code below.

To get the select count, we first pass the query string into $mysqli->query().

<?php
$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
    exit();
}

if ($result = $mysqli->query("SELECT * FROM artist")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.", $row_cnt);

    /* close result set */
    $result->close();
}

/* close connection */
$mysqli->close();
?>

If connection to the DB is successful, we should get the following screenshot displayed on the browser.

select count

We are using

select * from artist

and yet we are able to get the number of records in the table.  This is because we are using OOP way of programming.

$result = $mysqli->query("SELECT * FROM artist")

In OOP, a variable is an object.  After executing the above statement, the relevant methods and properties associated with the object could be used.

In this case we have used

$row_cnt = $result->num_rows;

to retrieve the number of records in the table artist.