I would like to create a file like this:
<?php
$cssMainVersion = "1.0.3";
$cssBootStrapVersion = "1.0.1";
$cssElseVersion = "1.0.4";
$jpgVersion = "1.0.1";
$jsMainVersion = "1.0.1";
$jsElseVersion = "1.0.1";
?>
I have created a mySQL table that holds the variable name, and the variable data. For example, the first row contains "cssMainVersion" and the second row contains "1.0.3".
How would I loop thorugh each row and print that data in a new file according to the format above?
You can create variables dynamically with php
$query = mysqli_query($conn, 'SELECT id, cachename, cacheversion FROM cache');
while($row = mysqli_fetch_assoc($query)) {
$$row['cachename'] = $row['cacheversion'];
}
the $$ will be evaluted from right to left, so, first $row['field'] will be replaced with this value, let say cssMainVersion, then we have $cssMainVersion
Related
First off all - a "normal" export and writing an Excel file using phpspreadsheet works like a charm.
What I can't find out is how to do this in a dynamic way.
"Dynamic" means not manually defining the headlines in row1 and not defining manually the data columns from mysql.
What I've done (tried):
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('test');
if ($result = $mysqli -> query($sql)) {
$co = 'A';
$ro = '1';
while ($fieldinfo = $result -> fetch_field()) {
// printf("$col.$row %s\n", $fieldinfo -> name);
$sheet->setCellValue($co.$ro, $fieldinfo -> name);
$$co = $fieldinfo -> name;
$co++;
}
$result -> free_result();
}
This creates an excel file with all field names like defined in the mysql view/statement.
The challenge is to put date in column by column without creating them by code.
What I've done is to generate a variable for each column. So $A = firstname, $B = name, etc.
My idea was to address this variable by something like "first column is 'A' then I've to address the variable $A. But dynamically call variables doesn't work somehow
$sheet->setCellValue($co.$ro, $row[$cuco]);
$cuco (current column) should be $cuco = '$'.$A, but then the value of $cuco will be $A and the resolved variable $A which should be "firstname" i.e.
What I've done is add a comment to each column in the database that corresponds to the heading I want for that column. An additional query will give you that data which you may then use to populate the header row.
Here's the function I use FWIW:
//
// Returns an array indexed by column names with the column comment,
// if any, as the value from the table name that is passed - columns
// that do not have a comment are not included in the returned array
//
public function get_column_comments($table) {
$ColsQ = $this->database->prepare('SHOW FULL COLUMNS FROM `' . $table . '`');
$ColsQ->execute();
$ColsD = $ColsQ->fetchAll(PDO::FETCH_ASSOC);
foreach ($ColsD as $col) {
if (empty($col['Comment'])) {continue;} // ignore columns without a comment
$cols[$col['Field']] = $col['Comment'];
}
return $cols;
} // end of get_column_comments function
i have created one table called role and the fields are like (roleid,role,prohibitedprocess,prohibitedports) here roleid is unique. and i have a comma separated value for "prohibitedprocess" field .(ex: prohibitedprocess---> skype,teamviwer,notepad).
the problem is how to add and edit and delete that comma separated value of prohibitedprocess .is it possible ?
i have fetch the code form the data base .code is
<?php
$sql = "SELECT roleid, prohibitedprocess, prohibitedports FROM role WHERE role='Basic'";
$option='';
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$pro_proc = $row['prohibitedprocess'];
$lst = explode(",",$pro_proc);
//Print in Option Box
foreach($lst as $exp)
{?>
<tr>
<td><?php echo $exp;?></td>
<td>Edit</td>
<td>Delete</td></tr>
<?php
}
}
?>
How to do this please help me
You can edit the whole string . .
I mean just using str_replace function
e.g
$string = "skype,teamviwer,notepad";
$old_value = "skype";
$new_value = "facebook";
$new_string = str_replace($old_value,$new_value,$string);
Then simply update this new string into database table. .
One way would be to retrieve all of 'keywords' in an input element and edit them like a sentence. However, for what you want to do, you may wanna add a new table called prohibitedprocess and use roleid as a foreign key. Then retrieve from that table according the roleid.
eg: SELECT item FROM prohibitedprocess p, role r WHERE p.roleid = r.roleid
It may be better to make another table for that relation, then you could edit it normally. Otherwise you can take your comma separated list and do something in javascript.
var commaSeparatedList = <?php echo $commaSeparatedList;?>
var realData = commaSeparatedList.split(',');
// when the user adds something you can add to the array or delete
// from the array
var newCommaList = readData.join([separator = ',']);
// some kind of get/post request that sends the new comma list
<?php
$newList = GET_REQUEST_PARAMS["newCommaList"];
// sql update query that updates the comma list
?>
I haven't used php in a while so I know GET_REQUEST_PARAMS isn't a thing but i'm sure you know how to get the parameters.
Or without javascript you can use a form that posts back all the names of prohibited things
<?php
$newList = ""
//foreach through the get params which contain the names
foreach()
{
$newList += getParam + ",";
}
// some code to remove that last comma
// sql update
?>
I have one html table with all the values from a specific table, and i want to put an update button to update specific rows
And i want to update those rows using checkboxes.
The user just need to check the selectboxes and click at a button, and this button will display all the information from the rows that were selected.
I have here a bit of code that i created, but i just can read the last selected box value.
I'm using MVC structure, but with my own rules.
Here the model, I created the function inside the model:
$editjo_query = ("SELECT * FROM my_table");
$print_editjo = db_array($editjo_query, 'a+'); //this is a function that i created to select, ignore please
if(!empty($_POST['editcheck'])){
$editcheckbox = $_POST['editcheck'];
$countCheckedit = count($_POST['editcheck']);
for($i=0;$i<$countCheckedit;$i++) {
$edit_id = $editcheckbox[$i];
$editjo_query = ("SELECT * FROM my_table WHERE id=$edit_id");
$print_editjo = db_array($editjo_query, 'a+'); //this is a function that i created to select, ignore please
}
}
And this function is being called at the view
function editjoview($print_editjo){
foreach($print_editjo as $check){
echo $check['id'];
}
}
you are always writing to $print_editjo with $print_editjo = db_array($editjo_query, 'a+'); and you you do not process it. So in ever for loop you overwrite it, leaving you with the last entry.
Try this:
$print_editjo = array();
for($i=0;$i<$countCheckedit;$i++) {
$edit_id = $editcheckbox[$i];
$editjo_query = ("SELECT * FROM my_table WHERE id=$edit_id");
$print_editjo[] = db_array($editjo_query, 'a+');
}
and in your view, iterate over the array $print_editjo rather than just having $print_editjo as the last entry.
Note: If you would add how exactly the function in your view is called, the help could be more precise.
I'm having a bit of trouble getting my retrieved values from an SQL query into the correct format.
I've managed to join multiple rows into the one value, however I am not sure how to make it separate each of the values with a comma. Essentially I need all the ID's of a product to be retrieved as, for example, if the database had values of '5,6,9,1' '1,3,4' and '2,1' I want it to throw a comma in between each like -> '5,6,9,1,1,3,4,2,1' instead is doing something more like -> '5,6,911,3,42,1' which is what it is doing at the moment.
The code I'm using is below. Any help would be greatly appreciated.
$hist = "SELECT ORDITEMS FROM cust_orderc WHERE ORDDATE >
to_date('".$olddate."','dd/mm/yyyy')";
$histitem = OCIParse($db, $hist);
OCIExecute($histitem);
while($row = oci_fetch_array($histitem)){
$pastitem .= $row['ORDITEMS'];
}
echo "$pastitem";
You can do same in oracle using LISTAGG
$hist = "SELECT LISTAGG(ORDITEMS) as ORDITEMS FROM cust_orderc WHERE ORDDATE > to_date('".$olddate."','dd/mm/yyyy')";
Edit OR PHP way
$pastitem = '';
while($row = oci_fetch_array($histitem)){
$pastitem .= $row['ORDITEMS'] . ',';
}
$pastitem = trim($pastitem, ",");
echo $pastitem;
I have the php script as follows :
$sql = "
SELECT
*
FROM
tbl_messages
WHERE
msg_sender_id = '$this->UM_index'
";
$res = $this->db->returnArrayOfObject($sql);
$this->assign_values('sent_messages',$res);
And in the templates I retrieved :
{foreach name = feach key = indx item = k from = $sent_messages}
{$k->msg_sender_id}
{$k->msg_subject}
{/forach}
I can retriev the above two fields. But I want to retrieve the message_sender_name for the field sender_id which can be obtained from a function getDetails($id).
My question is how can I assign the sender_name fields (which is a array) to my smarty template?
So you want also want to be able to have the message_sender_name, wich is returned by the function getDetails($id)?
You will have to loop trough your results, and fetch the message_sender_name for each row. Something like this:
$arrResults = array();
while ($res = $this->db->returnArrayOfObject($sql)))
{
$res['msg_sender_name'] = getDetails($res['msg_sender_id']);
$arrResults[] = $res;
}
$this->assign_values('sent_messages',$arrResults);
If I'm right, you can now access the sender_name via {$k->msg_sender_name} in your template-file.
But I would suggest that you use a JOIN in your SQL-statement, so you don't have to make another request for the sender_name.