I'm getting weird results with this block of code. foreach loop not doing the job, and while loop times out. Using PDO for queries
This is my code
public function alpha()
{
$alpha = NULL;
$sql = "SELECT
SUBSTRING(`last_name`, 1, 1) AS alpha,
SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
`id_clients`,
`type`,
`first_name`,
`middle_name`,
`last_name`,
`address`,
`primary_number`,
`secondary_number`,
`home_number`,
`office_number`,
`cell_number`,
`fax_number`,
`ext_number`,
`other_number`,
`comments`
FROM `clients`
WHERE `user_id` = 1
AND `is_sub` = 0
AND `prospect` = 1
ORDER BY `last_name`";
//$query = mysql_query ($sql) or die (mysql_error());
//echo "test";
foreach($this->db->fetch_row_assoc($sql) as $records) {
//while ($records = $this->db->fetch_row_assoc($sql)) {
$alpha[$records['alpha']] += 1;
${$records['alpha']}[$records['id_clients']] = array(
$records['first_name'], //item[0]
$records['subMiddleName'], //item[1]
$records['last_name'], //item[2]
$records['address'], //item[3]
$records['primary_number'], //item[4]
$records['secondary_number'], //item[5]
$records['home_number'], //item[6]
$records['office_number'], //item[7]
$records['cell_number'], //item[8]
$records['fax_number'], //item[9]
$records['ext_number'], //item[10]
$records['other_number'], //item[11]
$records['comments'], //item[12]
$records['type'] //item[13]
);
}
If I use the while loop then my page locks up and throws out a Fetal Error with a 60 sec timeout message. If I use the foreach loop then It seems to go thru but now my results are ...weird...
These results are from using the foreach loop with a PDO query
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
B
B B B
B
primary: (B)
secondary: (B)
B
E
E E E
E
primary: (E)
secondary: (E)
E
O
O O O
O
primary: (O)
secondary: (O)
O
These are the results I should be getting. These results are from using the while loop with mysql queries
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
O
Eli O Orellana
5542 Crossover Prk
primary: 000.000.0000 (cell)
secondary: 000.000.0000 (home)
Buyer
Sandra Y Orellana
123 Maple Ln
primary: 000.000.0000 (cell)
secondary: 000.000.0000 (home)
Seller
Josue Orellana
primary: (Home)
secondary: (Cell)
Buyer
EDIT: Rest of code
// Create Alpha link Listing
foreach(range('A','Z') as $i) {
echo (array_key_exists ("$i", $alpha)) ? '<a href="#'.$i.'" title="'.$alpha["$i"].' results">'.$i.'</a>' : "$i";
echo ($i != 'Z') ? ' | ':'';
}
// Create Data Listing
foreach(range('A','Z') as $i) {
开发者_如何学Go if (array_key_exists ("$i", $alpha)) {
echo '<div class="alpha"><span class="expand-alpha-up"></span><a href="#" name="'.$i.'">'.$i.'</a></div><div class="show">';
foreach ($$i as $key=>$item)
echo '
<table cellpadding="0" cellspacing="0" width="99%">
<tr>
<td class="td-name"><p><a href="#" id="'.$key.'">'.Truncate($item[0].' '.$item[1].' '.$item[2], 17, true).'</a></p></td>
<td class="td-address"><p>'.Truncate($item[3], 20, true).'</p></td>
<td class="td-contact"><p>primary: '.($item[4] == 'home' ? $item[6] : ($item[4] == 'office' ? $item[7] : ($item[4] == 'cell' ? $item[8] : ($item[4] == 'fax' ? $item[9] : ($item[4] == 'other' ? $item[11] : ''))))).'<i> ('.$item[4].')</i></p></td>
<td class="td-contact"><p>secondary: '.($item[5] == 'home' ? $item[6] : ($item[5] == 'office' ? $item[7] : ($item[5] == 'cell' ? $item[8] : ($item[5] == 'fax' ? $item[9] : ($item[5] == 'other' ? $item[11] : ''))))).'<i> ('.$item[5].')</i></p></td>
<td class="td-type"><p><strong>'.$item[13].'</strong></p></td>
</tr>
</table>';
}
echo '</div>';
}
}
}
Also my fetch() method
public function fetch_row_assoc($statement) {
self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
try
{
$stmt = self::$PDO->query($statement);
//$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
}catch(PDOException $e){
echo $e->getMessage();
}
return false;
}
Try using this PDO helper class: PDO Helpers Class on Github
Pass the query string to getQuery()
require_once("Database.php");
$db = new DB;
$data = $db->getQuery($sql);
foreach($data as $d)
{
$d['first_name']; // etc
}
see if that helps.
精彩评论