MySql alphabetical rows listing in a loop while assigning variables.

I am pulling results for each letter of the alphabet that sorts and lists in alphabetical order. The example here is for the letter M. The code is intended to pull all results that begin in M and list them alphabetically. Each row that results is used to populate the “echo section” listed and then repeat. I am unable to locate anywhere that explains how to pull row result and assign a variable without terminating the loop and only returning a single row (the first result). I can pull one row and have it display correctly, but I am stumped how to pull multiple row results and assign them to the Echo section and then move onto the next alphabetic result.

Any help is appreciated! A location with an explanation would also be appreciated.

[php]

<?php require("cnfgcnnt.php"); if($dbcon){ $query = "SELECT * FROM catalg ORDER BY name AND name LIKE 'M%'"; $results = mysqli_query($dbcon, $query); if($row = mysqli_fetch_array($results)){ // Should the if command be a while? and should there be a >0 in the previous command? // Returning all rows with an M in alphabetic order // Variables used in creating the output // If I assign the varaibles here, only the first row that meets the criteria is displayed // $date = $row['date']; // $name = $row['name']; // $define = $row['define']; // $submitter = $row['app_id']; // $website = $row['website']; // $email = $row['email']; } } else { die(mysqli_error()); } // The output variables populate some hyperlinks // There are several table enties that should repeatedly use the output below, until all the M's are used. // how do I assign the results to display in the echo without terminating the output after a single row? //{ // echo "Name: " .$row['name']; // echo "Definition: " . $row['define']; // echo "

\n"; echo " $name - $submitter - $email/$define \n"; echo "

\n"; ?>

[/php]

This is not a valid query

[php]$query = “SELECT * FROM catalg ORDER BY name AND name LIKE ‘M%’”;[/php]

It should be something like this

[php]$query = “SELECT * FROM catalg WHERE name LIKE ‘M%’ ORDER BY name ASC”;[/php]

Thanks Matt.

I made the changes, however, I still am only getting one result and the loop stops.

I am trying to get the rows to repeat and output a list of repeating graphics with hyperlinks and text hyperlinks in alphabetical order.

[php]<?php

require(“cnfgcnnt.php”);
if($dbcon){
$query = “SELECT * FROM catalg WHERE name LIKE ‘M%’ ORDER BY name ASC”;
$results = mysqli_query($dbcon, $query);
if($row = mysqli_fetch_array($results)){
// Should the if command be a while? and should there be a >0 in the previous command?
// Returning all rows with an M in alphabetic order

// Variables used in creating the output
// If I assign the varaibles here, only the first row that meets the criteria is displayed
$date = $row[‘date’];
$name = $row[‘name’];
$define = $row[‘define’];
$submitter = $row[‘app_id’];
$website = $row[‘website’];
$email = $row[‘email’];

}

} else {
die(mysqli_error());
}

// The output variables populate some hyperlinks
// There are several table enties that should repeatedly use the output below, until all the M’s are used.

// how do I assign the results to display in the echo without terminating the output after a single row?
//{
// echo "Name: " .$row[‘name’];
// echo "Definition: " . $row[‘define’];
//

echo “

\n”;
echo " <span style=“font-size:18px;”>$name - <a href=“http://$website/” target="_blank">$submitter - $email/$define \n";
echo “

\n”;

?>[/php]

I am trying to understand if I am supposed to be using the for each statement.

Is this getting closer?

[php]
require(“cnfgcnnt.php”);

if($dbcon){
$query = “SELECT * FROM catalg WHERE name LIKE ‘M%’ ORDER BY name ASC”;
$results = mysqli_query($dbcon, $query);
if($row = mysqli_fetch_array($results)){
// Should the if command be a while? and should there be a >0 in the previous command?
// Returning all rows with an M in alphabetic order

// Variables used in creating the output
// If I assign the varaibles here, only the first row that meets the criteria is displayed

foreach ($results as $result): 	 

// $result[‘name’];
// $result[‘define’];
// $result[‘submitter’];
// $result[‘website’];
// $result[‘email’];

echo “

\n”;
echo " <span style=“font-size:18px;”>.$result[‘name’]. - <a href=“http://.$result[‘website’]./” target="_blank">.$result[‘submitter’]. - .$result[‘email’]./.$result[‘define’]. \n";
echo “

\n”;

}

} else {
die(mysqli_error());
}
[/php]

[php]
whlie($row = mysqli_fetch_assoc($results)) {
$date = $row[‘date’];
$name = $row[‘name’];
$define = $row[‘define’];
$submitter = $row[‘app_id’];
$website = $row[‘website’];
$email = $row[‘email’];
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service