i working on Authorize.net gateway i need to create below array for itemized bill
$sql= "select * from shop where cusid = '1'";
$sqlexc= mysql_query($sql) or die(mysql_error());
$line_items = array();
while ($title = mysql_fetch_array($sqlexc)) {
$line_items[] = ('Coupon',' $title[6]', '1', '0.99', 'Y');
}
how to create below kind of array
/* $line_items = array(
"Coupon1','description','2','10.99','Y'",
"Coupon2','description','2','10.99','Y'",
"Coupon3','description','2','10.99','Y'",);*/
uh.. if you want it to be exactly the same...
$i=0;
$line_items = array();
while ($title = mysql_fetch_array($sqlexc)) {
$line_items[] = "Coupon" . ++$i . "', '" . $title[6] . "', '2', '10.99', 'Y'";
}
However I would use mysql_fetch_assoc over mysql_fetch_array (actually I would switch to PDO as mysql is deprecated)
$line_items = array();
while ($title = mysql_fetch_array($sqlexc)) {
$line_items[] = $title;
}
foreach ($line_items as $row){
echo $row[0]." ".$row[1]." ".$row[2]." ".$row[3]."<br>";
}
Related
I am trying to execute the following query using CodeIgniter, but I am getting
Unidentified index for ->row['code'];
public function getSExtension($id) {
$temp = array();
$query = $this->db->query("SELECT extension_id FROM " . DB_PREFIX . "zipcode_shipping WHERE `zip_code` = '" . $id . "'");
if($query->num_rows) {
foreach ($query->rows as $key => $value) {
$code = $this->db->query("SELECT code FROM " . DB_PREFIX . "extension WHERE `extension_id` = '" . $value['extension_id'] . "'")->row['code'];
$this->language->load("shipping/".$code);
$temp[] = $this->language->get('text_title');
}
}
return $temp;
}
The problem is you have not correctly retrieved a result set which requires a method call. As you have it you are trying to use db properties that don't exist i.e. $query->rows should probably be $query->row_array().
Documentation Here.
In the example below I use result() and row() instead of any "array" methods.
Try this.
$temp = array();
$query = $this->db->query("SELECT extension_id FROM ".DB_PREFIX."zipcode_shipping WHERE `zip_code` = '".$id."'");
if($query->num_rows() > 0)
{
foreach($query->result() as $value)
{
$code = $this->db->query("SELECT code FROM ".DB_PREFIX."extension WHERE `extension_id` = '".$value->extension_id."'")->row()->code;
$this->language->load("shipping/".$code);
$temp[] = $this->language->get('text_title');
}
}
Need to find array, and then run a MYSQL SELECT where array values are present (or not present).
$symbol = "abc";
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
$stop = preg_replace("/[0-9]/", "", $row['tip']);
if ($stop == $symbol)
{$sword = $row['tip'];
}}
So we need $sword to serve as an array in the event that there are multiple outputs. After we have that array, we need to run a mysql query that shows only those that have $sword array.
$query = "
SELECT * FROM ms WHERE `big` = '$sword'";
$result = mysql_query( $query );
So then we can do something like:
while ( $row = mysql_fetch_assoc( $result ) ) {
echo '"time": "' . $row['time'] . '",'; }
Here's the code, converting $sword to an array and using it in your 2nd query:
$symbol = array("abc", "def", "ghi");
$sword = array();
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
$stop = preg_replace("/[0-9]/", "", $row['tip']);
if (in_array($stop, $symbol)) {
$sword[] = $row['tip'];
}
}
if ($sword) {
$in = '';
$sep = '';
foreach ($sword as $s) {
$in .= "$sep '$s'";
$sep = ',';
}
/* $in now contains a string like "'123abc123', '12abc12', '1abc1'" */
$query = "
SELECT * FROM ms WHERE `big` IN ($in)";
$result = mysql_query( $query );
while ( $row = mysql_fetch_assoc( $result ) ) {
echo '"time": "' . $row['time'] . '",';
}
}
Now I'm going to go wash my hands to get all that mysql_query off me... :-)
I am making a script which will load all categories from a database. I just don't know how I can do this in a way which is shorter.
I am trying to figure out a way in which I can theoretically load infinite levels of categories.
$stmt1 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek IS NULL");
while($row1 = $db->fetch($stmt1))
{
$stmt2 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row1['Rubrieknummer'] . "'");
while($row2 = $db->fetch($stmt2))
{
$stmt3 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row2['Rubrieknummer'] . "'");
while($row3 = $db->fetch($stmt3))
{
$stmt4 = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek = '" . $row3['Rubrieknummer'] . "'");
while($row4 = $db->fetch($stmt4))
{
$row3['CATEGORY3'][] = array(
'CATEGORY4_NAME' => ucfirst(strtolower($row4['Rubrieknaam'])),
);
}
$row2['CATEGORY3'][] = array(
'CATEGORY3_NAME' => ucfirst(strtolower($row3['Rubrieknaam'])),
'CATEGORY4' => $row2['CATEGORY4'],
);
}
$row1['CATEGORY2'][] = array(
'CATEGORY2_NAME' => ucfirst(strtolower($row2['Rubrieknaam'])),
'CATEGORY3' => $row2['CATEGORY3'],
);
}
$catagories[] = array(
'CATEGORY_NAME' => ucfirst(strtolower($row1['Rubrieknaam'])),
'CATEGORY2' => $row1['CATEGORY2'],
);
}
I would like to have some ideas on how to make this happen.
(I am using a little template system that processes the array that is being made)
EDIT:
Thanks to Ivijan Stefan Stipić I was able to solve this.
This is what I ended up doing
$categories = build_category(0);
And the function:
function build_category($parent, $row = NULL)
{
global $db;
// Initialise array
$data = array();
// Basic SQL statement
$sql = "SELECT * FROM Rubriek";
// Where condition based on $row
if(is_null($row))
{
$where = " WHERE Hoofdrubriek IS NULL";
}
else
{
$where = " WHERE Hoofdrubriek = '" . $row['Rubrieknummer'] . "'";
}
// Execute query
$stmt = $db->query($sql . $where);
// Next level parent
$next = $parent + 1;
// Fetch results
while($row = $db->fetch($stmt))
{
$data[] = array(
'CATEGORY' . $parent . '_NAME' => ucfirst(strtolower($row['Rubrieknaam'])),
'CATEGORY' . $next => build_category($next, $row),
);
}
// Return data
return $data;
}
What do you think about this solution?
function next_level($val)
{
global $db;
$stmt = $db->query("SELECT * FROM Rubriek WHERE Rubrieknummer != '" .$val. "' AND Hoofdrubriek = '" .$val. "'");
while($row = $db->fetch($stmt))
{
echo ucfirst(strtolower($row['Rubrieknaam']));
echo next_level($row['Rubrieknummer']);
}
}
$stmt = $db->query("SELECT * FROM Rubriek WHERE Hoofdrubriek IS NULL");
while($row = $db->fetch($stmt))
{
echo ucfirst(strtolower($row['Rubrieknaam']));
echo next_level($row['Rubrieknummer']);
}
If I understand correctly this is the number of categories with unlimited subcategories.
I am just a beginner in php. I used the following code to display the user names fetched from the database.
$select_tl = "SELECT `varTeamleader` FROM `tbl_team` WHERE `intTeamid` = '" . $id . "'";
$select_tl_res = mysql_query($select_tl);
$select_tl_num = mysql_num_rows($select_tl_res);
if ($select_tl_num > 0) {
$fetch_tl = mysql_fetch_array($select_tl_res);
$tl = $fetch_tl['varTeamleader'];
$sep_tl = explode(",", $tl);
foreach ($sep_tl as $key => $value) {
$sel_tlname = "SELECT * FROM `tbl_user` WHERE `intUserid` = '" . $value . "'";
$sel_tlname_res = mysql_query($sel_tlname);
$sel_tlname_num = mysql_num_rows($sel_tlname_res);
if ($sel_tlname_num > 0) {
$fetch_username = mysql_fetch_array($sel_tlname_res);
$user_name = $fetch_username['varName'];
echo $user_name . ", ";
}
}
}
I need to echo the user_name with comma after every value but not after last one. How can i do that?
Add the items to an array and then implode(',', $arr).
After you're done iterating, remove the last comma. That's an easy way of doing it.
$user_name=substr($user_name,0,-2)
I'm having great trouble with "INSERT INTO"...
I have a variable part number so this my code...:
<?php
include ("db_conn.php");
$mem_id = "1";
$descript = "chair";
$qualifier = "sitting";
$major = "Y";
$value = "6";
//$mesh_cell_string = "tree_0,tree_1,tree_2,tree_3,tree_4";
//$mesh_values_string = "'C23','550','291','687','500'";
$part_number = "C23.550.291.687.500";
$parts = explode('.', $part_number);
$n = 0;
foreach ($parts as $something => $number)
{
$mesh_cell_string .= "tree_" . $n . ",";
$mesh_values_string .= "'" . $number . "'," ;
$n++;
}
$mesh_values_string = substr($mesh_values_string, 0, -1);
$mesh_cell_string = substr($mesh_cell_string, 0, -1);
$insert_string = "mem_id,mesh_heading_name," . $mesh_cell_string . ",qualifier_name,major,rank";
$values_string = "'$mem_id','$descript'," .$mesh_values_string. ",'$qualifier','$major','$value'";
$sql = "INSERT INTO mesh_table (" . $insert_string .") VALUES (" . $values_string .")";
$result = mysqli_query($cxn,$sql) or die ("couldn't execute the query");
?>
The strange thing is... i don't get an error ("couldn't execute the query") so i thought it went alright but when i look into my database there aren't any values written... when i un-comment the the 2 variables:
//$mesh_cell_string = "tree_0,tree_1,tree_2,tree_3,tree_4";
//$mesh_values_string = "'C23','550','291','687','500'";
And comment the foreach loop, it works...? So there goes something wrong in the foreach loop, but when i echo the $sql on both methods i get the same:
INSERT INTO mesh_table (mem_id,mesh_heading_name,tree_0,tree_1,tree_2,tree_3,tree_4,qualifier_name,major,rank) VALUES ('1','Chair','C23','550','291','687','500','sitting','Y','6')
I really don't know what i am doing wrong...?
Best regards,
Thijs
change $values_string = "'$mem_id','$descript'," .$mesh_values_string. ",'$qualifier','$major','$value'";
To
$values_string = "'".$mem_id."','".$descript."'," .$mesh_values_string. ",'".$qualifier."','".$major."','".$value."'";