How can I copy the content of one column to another one? - php

I have following table structure:
// Posts
+----+---------+-----------------+------------------+------------------------+------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+-----------------+------------------+------------------------+------------------------+
| 1 | title1 | <b>content1</b> | **content1** | <b>content1_edited</b> | **content1_edited** |
| 2 | title2 | <i>content2</i> | *content2* | | |
+----+---------+-----------------+------------------+------------------------+------------------------+
As you see in the above table, I just keep two last edit (content(html/markdown) is one before the last edit or the content without editing and content_edit(html/markdown) is last edit or empty if there isn't any edit)
Now please take a look at these two examples:
Example One: I want to edit first post which is edited already. Here is new value:
**content1_new_value**
Well I also convert it to html using a PHP library and make this:
<b>content1_new_value</b>
I want to fill (update) the content of content_(html/markdown) with the current content of content_edit_(html/markdown) and fill (update) this column content_edit_(html/markdown) with new value, something like this:
+----+---------+------------------------+----------------------+----------------------------+------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+------------------------+----------------------+----------------------------+------------------------+
| 1 | title1 | <b>content1_edited</b> | **content1_edited** | <b>content1_new_value</b> | **content1_new_value** |
+----+---------+------------------------+----------------------+----------------------------+------------------------+
Example Two: I want to edit second post which is not edited already. Here is new value:
*content2_new_value*
Well I also convert it to html using a PHP library and make this:
<i>content2_new_value</i>
In this case I want to keep content_(html/markdown) intact (without any change) and just update content_edited_(html/markdown) with new value, something like this:
+----+---------+-----------------+------------------+------------------------+--------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+-----------------+------------------+------------------------+--------------------------+
| 2 | title2 | <i>content2</i> | *content2* | *content2_new_value* | <i>content2_new_value</i>|
+----+---------+-----------------+------------------+------------------------+--------------------------+
So as you see, I just keep the two last edit. How can I do that?

Check CASE WHEN ELSE END
case
when content_edit_html is not null
update table set content_html = content_edit_html, content_markdown = content_edit_markdown,
content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
else
update table set content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
end

Related

How to store css into mysql database and call it back to be displayed on html

I would like to store my css in mysql database. But how should I suppose to store it. And how to call it back to be displayed on html page.
Note: I just want to store css in mysql database only
I am not sure if storing CSS in database is a good idea, but since you are explicitly requesting for it then you might follow this approach.
TABLE: styles
| id | selector | style_name |
|----|----------|--------------|
| 1 | body | align-center |
| 2 | .d-flex | display-flex |
| 3 | #badge | custom-badge |
TABLE: styles_css
| id | style_name | property | value |
|----|--------------|--------------|-----------|
| 1 | align-center | text-align | center |
| 2 | display-flex | display | flex |
| 3 | custom-badge | color | red |
| 4 | custom-badge | font-size | 1.5rem |
Then when you need the styles for your HTML, you can join these tables to get CSS styles.
SELECT `property`, `value`
FROM `styles_css`
LEFT JOIN `styles`
ON `styles`.`style_name` = `styles_css`.`style_name`
WHERE
`styles`.`selector` = '#badge';
This will give you
| property | value |
|------------|----------|
| color | red |
| font-size | 1.5rem |
Example:
<?php
// prepare statement and bind params(PDO would be a good option)
$sql = "
SELECT `property`, `value`
FROM `styles_css`
LEFT JOIN `styles`
ON `styles`.`style_name` = `styles_css`.`style_name`
WHERE
`styles`.`selector` = '#badge';
";
//after execution, something like $styles = $db->exec($sql);
foreach($styles as $property => $value) {
$css = "{$property} : {$value}; ";
}
// remove trailing semi-colon
$css = rtrim($css, ';');
?>

Remove values in comma separated list from database on CodeIgniter

Edit :
I give another example,
products_table
| ID | product_name | images |
|---------------------------------------|
| 1 | Phone | img1.jpg,img2.jpg
| 2 | Canon | img1.jpg,img2.jpg,img3.jpg
| 3 | Cocac | img1.jpg,img2.jpg,img3.jpg,img4.jpg
| 4 | Lamps | img1.jpg,img2.jpg,img3.jpg,img4.jpg
Lats say I remove image "img3.jpg" from Lamps product without remove other images. What is the SQL call to this?
$this->db->query("UPDATE products_table SET images=replace(images, 'img3.jpg,',''),images=replace(images, ',img3.jpg','') WHERE product_name='Lamps'");

Show rows with a column partially matching a given parameter

Suppose I have a table named testdb which has the following values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
| Alex ghosh | 8985855665 | 4 |
Now, I am performing a search by the name and the input given is just "supriyo" (no last name given).
The search result should show both the values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
Is this possible to achieve using MySQL and php?
You are looking for LIKE:
select *
from your_table
where name like 'supriyo%';
Demo
Edit:
In php, use:
select *
from your_table
where name like '$name%';

Fetching Record Pattern From Database

I have a table like this in my database.
| id | yes_answer | no_answer |
| a01 | a05 | a04 |
| a03 | a16 | a32 |
| a04 | c01 | a05 |
| a05 | a06 | c06 |
| a06 | a08 | a09 |
| a08 | c03 | a09 |
| a09 | a03 | |
| a16 | a33 | a02 |
| a32 | c04 | a16 |
| a33 | c05 | |
As An Explanation :
1.id column represent an id of question. it is linked to another table on my database.
2.yes_answer column represent the next question (like a01) or conclusion (like c04) you get when you answer the question represented by id column with yes or TRUE or 1 value.
3.no_answer is generally the same as yes_answer column except that you get it when you answer question represented by id column with no, FALSE, 0 value.
4. Every id, yes_answer, no_answer is connected each other. The Result is A Pattern. When you Want to find a path that lead to answer c01 it will be like c05 -> a33,a16,a32,a03,a09,a06,a05,a01.
And Here I am Trying To Bactrack The Pattern From answer column that marked with c like c01 or c02 like above. And I've been able to bactrack only the beginning value of it Like (c05 -> a33) and the rest is blank.
Here is My Code
$sqlQueryYes="SELECT * FROM tbl_path_inference WHERE yes_answer='c05'";
$execQueryYes=mysql_query($sqlQueryYes);<br />
$queryStatus=mysql_num_rows($execQueyYes); //Getting Query Status<br />
if($queryStatus == 1) {
$getDataYes=mysql_fetch_array($execQueryYes);
$ansPath=$ansPath." ".$getDataYes['id'];
} else { // if cant find answer on column yes_answer try on no_answer
$sqlQueryNo="SELECT * FROM tbl_path_inference WHERE no_answer='c05'";
$execQueryNo=mysql_query($sqlQueryNo);
$getDataNo=mysql_fetch_array($execQueryNo);
$ansPath=$ansPath." ".$getDataNo;
}
Anyone Have Solution On It. Im Still working It Now.

mysql query how to fill in blank rows using the value of its top row with content?

I need a mysql query to do this
But I can't think of something to make this happened
We have an excel file and that contains more than 5,000 rows
some are blank and others have value what I manually do is
to copy top value to its below blank fields and so on
so I think a way that I can import the excel into myslq database
and make some query to save effort and time in terms of doing this..
and this is my table looks like:
only column Date-paid has value for the entire rows but some has value and other don't have see this example but the original file is more than 5,000 rows
|Name | age | Salary| Date_paid |
-----------------------------------
bbjake| 16 | 200 | 03/25/2015 |
----------------------------------|
| | | 04/25/2015 |
----------------------------------
| | | 05/25/2015 |
-----------------------------------
kentot| 20 | 300 | 03/25/2015 |
-----------------------------------
| | | 04/25/2015 |
----------------------------------|
| | | 05/25/2015 |
-----------------------------------
and after running the query this is my desired result
|Name | age | Salary| Date_paid |
----------------------------------|
bbjake| 16 | 200 | 03/25/2015 |
-----------------------------------
bbjake| 16 | 200 | 04/25/2015 |
-----------------------------------
bbjake| 16 | 200 | 05/25/2015 |
-----------------------------------
kentot| 20 | 300 | 03/25/2015 |
-----------------------------------
kentot| 20 | 300 | 04/25/2015 |
-----------------------------------
kentot| 20 | 300 | 05/25/2015 |
-----------------------------------
So blank fields will be filled in with the value on the raw with content and
drag down until with another raw with value in short will fill in blank rows with
the content of top row
table is manually imported into the database and let just say columns are only three Name(varchar) Age(int) Salary (int) Date Paid (Date) Table name = "Emplyoee" Database name = "Company"
your question is strange...why would somebody wanna do this?
Anyway, to do this, it's interesting, how this table is created / generated. Your question is very weird...i think you want to know, how to fill this table AFTER the server built it and created the HTML table (because of your post title here). On the other side, this "php" tag does not make any sense in this scope....
JavaScript solution:
var trs = document.getElementsByTagName('tr');
var innerHTML = "";
for (var i = 0; i < trs.length; i++){
if (trs[i].className !== "heading") {
innerHTML = (trs[i].innerHTML.children[0] == "") ? innerHTML : trs[i].innerHTML;
trs[i].innerHTML = innerHTML;
}
}
note that you have to set the classname of the description rows to "heading" in this example, that this will not be interpreted from the script. If your server alone shall do this, just follow the same logic like in my JavaScript. Always check, if next element would be empty - if yes: fill in the same string like before; if not: take the new string and fill it into all next empty cells, etc....

Categories