How to get data from data table using php - html

i am learning php nad html and mssql
Hello i dont know anything wrong can someone explain me?
+(everything configured righ in db i guess)
<?php
include('Database\DBconnect.php');
?><?php
$procs = "{call p_bas_sel_DataTest()}";
$paramss = array();
while($getquestionsfromphp=sqlsrv_fetch_object($results)){ ?>
Now HTML
<label class="questlabel">1.blah blah?</label>
<div class="answers" id="ans1" style="margin-left: 2em">
<button ><label ><span ><?php echo $getquestionsfromphp['D_Value'];?></span></label></button>
</div><?php } ?>

<?php
$sql = "SELECT * FROM Data_Test WHERE lang='M'" ;
$query = sqlsrv_query ($conn,$sql);
$arr = array();
while ($row = sqlsrv_fetch_array($query)){
$array[] = $row;
}
?>
I fixed my error coding like this i put data into array from database then used on some text as index numbers

Related

How can i solve my codeigniter batch update problem?

Hello guys I just want to ask in my project there are three tables product, color and product_color.I insert database using insert_batch then it work fine, when i update product_color table using update_batch then face some problems.Here's my sample code:
Database:
product:id,name,sku...
color:id,color_name
product_color:id,pro_id,color_id
Input Form:
<?php foreach($colors as $color): ?>
<input type="checkbox" class="form-check-input" name="color[]" value="<?php echo $color->color_id; ?>" <?php foreach ($productcolor as $key => $value){ $array[] = $value->color_id;} if(in_array($color->color_id,$array)) echo 'checked'; else ''; ?>>
<label class="form-check-label">
<?php echo $color->color_name; ?>
</label>
<?php endforeach; ?>
Actually i want to pass primary id from product_color table.Here i pass color_id.Have any way to pass primary id from input form;
Here Is my Controller:
$colorBatch = array();
foreach ($color as $colorvalue) {
$colorBatch[] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');
Where $pid contains product_id;
Is it possible to pass product_color table primary id from input form or Have any better solution to solve this.Sorry for bad english.
Thanks
Please check below code as your array structure is wrong;
$colorBatch = array();
foreach ($color as $key => $colorvalue) {
$colorBatch[$key] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');

How to display Name in codeigniter

Please help me, I want to display the name of the users from the table according to sender_id and recipient_id, how do the code model and view in my CodeIgniter
Tables schema
Data Table users
Data Table Messages
Model
function get_pesan_view(){
$this->db->from('messages');
$this->db->join('users', 'users.id = messages.sender_id', 'left');
$this->db->join('users', 'users.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
}
Contoller
public function view_pesan()
{
$data['get_pesan']=$this->Emp_model->get_pesan_view();
$this->load->view('employee/top');
$this->load->view('employee/nav', $data);
$this->load->view('employee/slidbar', $data);
$this->load->view('employee/pesan', $data);
$this->load->view('employee/bottom');
}
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->name ; ?> </strong>
<strong> Recipient : <?php echo $pesan->name ; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
I want to display the name of the sender and recipient together in view
Controller
public function get_names(){
$this->load->model('model_name'); //If not loaded in the autoload.php file
$names = $this->model_name->all_names();
$this->load->view('view_file_name', compact('names'));
}
The query in the model will be like this
public function all_names(){
$this->db->select('name');
$this->db->from('users');
$this->db->join('messages', 'users.id = messages.sender_id');
return $this->db->get()->result();
}
To join with recipient_id do like this
$this->db->join('messages', 'users.id = messages.recipient_id');
Then use foreach loop to show all names in the view
foreach($names as $row){
echo $row->name;
}
model
$this->db->select('messages.*,u1.{name of field for username} as sender_name,u2.{name of field for username} as recipient_name');
$this->db->from('messages');
$this->db->join('users u1', ' u1.id = messages.sender_id', 'left');
$this->db->join('users u2', ' u2.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->sender_name; ?> </strong>
<strong> Recipient : <?php echo $pesan->recipient_name; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
i have forgotten you cannot join one tale more than one time you need to use table aliases. it will work for you/ just place the name what ever fild name is in your data base.
it will work or else share me screen short of your table and error pages

How can I group subtopics into topics in this MySQL statement?

I've tried GROUP BY with my data below but it only brings back one subtopic. How can I return all the subtopics and organise them under each topic without the topic_name appearing with each subtopic_name.
Edit: Included a screenshot of the page and here is the PHP used:
<ul class="topics-list">
<?php
foreach ($data as $key){
foreach ($key as $item){
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
?>
<div class="the_topic">
<h2 class="topic_change"><?php echo $topic_name; ?></h2>
<ul><li class="subtopic_name"><h3><?php echo $subtopic_name; ?></h3></li></ul>
<hr />
</div>
<?php } ?>
<?php } ?>
</ul>
You could use GROUP_CONCAT() to concatenate all subtopics into one string per topic, and then parse the string in your application code.
SELECT topic_name, GROUP_CONCAT(subtopic_name DELIMITER 'ยงยงยง') as subtopic_names
FROM questions2
GROUP BY topic_name
But i do not recommend that, because you will get in troubles, if a subtopic contains your delimiter. I would just use your second query and group the result in the application code.
PHP code would look something like:
// group the data
$groupedData = array();
foreach ($data as $item) {
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
$groupedData[$topic_name][] = $subtopic_name;
}
// grouped output
foreach ($groupedData as $topic_name => $subtopic_names) {
echo '<div class="the_topic">';
echo '<h2 class="topic_change">' . $topic_name . '</h2><ul>';
foreach ($subtopic_names as $subtopic_name) {
echo '<li class="subtopic_name"><a href="#" data-toggle="modal" data-target="#lvlModal"><h3>';
echo $subtopic_name;
echo '</h3></a></li>';
}
echo '</ul><hr /></div>';
}

Populating Drop Down List from Previous Drop Down - using AJAX, PDO, jQuery

I'm brand new to using PHP and mySQL so I'm fully aware I've probably made some very 'noob' errors. However, I'm really stuck when it comes to populating my second drop down list from mySQL, when an option has been selected from a previous drop down list.
I've tried all the forums and have tried example scripts, but each time, I continue to get errors despite trying different examples on the internet. Thus hoping why I wish someone could help me out.
I'm trying to build a small film website, whereby someone can select a film, and then select a date & time and etc. I'm using mySQL to populate the drop down lists but get stuck when I want to populate the second dropdown, with it being dependent on the first option select.
I've tried using AJAX but to no avail. Appreciate I'm probably made some very rookie errors here (possible confusion around variables) but if someone could shed some light on where I'm wrong, I would be really grateful.
You have small mistake on getDates.php
<?php
require("Connect.php");
$name = (!empty($_REQUEST["name"])) ? trim($_REQUEST["name"]) : "");
if (!empty($name)) {
$sql = "SELECT Dates FROM Cinema WHERE name = $name";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue("tn", trim($name));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo($ex->getMessage());
}
if (count($results) > 0) {
?>
<label>Dates:
<select name="Dates">
<?php foreach ($results as $rs) { ?>
<option value="<?php echo $rs["Dates"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
}
?>
Try it please and feedback
index.php
<?php
require Connect.php;
?>
<!DOCTYPE html>
<html>
<head>
<title>Films for Family!!</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function Datesforfilms(sel) {
var name = sel.options[sel.selectedIndex].value;
$("#output1").html("");
$.ajax({
type: "POST",
url: "getDates.php",
data: "name=" + name,
cache: false,
success: function(html) {
$("#output1").html(html);;
}
});
}
</script>
</head>
<body>
<div class="welcome">
<h1>Films for Family</h1>
</div>
<div class="SelectFilm">
<h3> Step 1: </h3>
<p> *Select a film*</p>
<form name="Filmform" method="get">
<select name="Select a Film" onChange="Datesforfilms(this);">
<option value="0">Select a Film</option>
<?php
$sql = "SELECT name FROM company";
$handle = $conn->prepare($sql);
$handle->execute(array($sql));
$res = $handle->fetchAll();
foreach($res as $movie) { ?>
<option value="<?=$movie['name']?>"><?=$movie['name']?></option>
<?php } ?>
</select>
<div id="output1"></div>
</form>
</div>
</body>
</html>
getDates.php
<?php
require("Connect.php");
$name = (!empty($_POST["name"]) ? trim($_POST["name"]) : "");
if (!empty($name)) {
$sql = "SELECT Dates FROM Cinema WHERE name = $name";
try {
$stmt = $DB->prepare($sql);
$stmt->bindValue("tn", trim($name));
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo($ex->getMessage());
}
if (count($results) > 0) {
?>
<label>Dates:
<select name="Dates">
<?php foreach ($results as $rs) { ?>
<option value="<?php echo $rs["Dates"]; ?>"><?php echo $rs["Dates"]; ?></option>
<?php } ?>
</select>
</label>
<? } ?>
<? } ?>

Pull content from WordPress site to display on HTML site [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a WordPress website with blogs on it, but I also have a HTML/Dreamweaver site.
I'd like to have the WordPress blogs automatically display on my index.html page when I update the WordPress website.
If both sites hosted on same server, it is simply possible. First you have to make a PHP file for example latest-post.php. and add the code below
<?php
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php");
?>
<?php
//The Loop - latest 5 posts from blogs category
$query1 = new WP_Query('showposts=5&cat=blogs&offset=0');
if ($query1->have_posts()) :
while ($query1->have_posts()) : $query1->the_post();
?>
<div class="whatever you want to style">
<h2><!--The Title-->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h2>
<!--thumbnail-->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('Thumbnail'); ?>
</a>
<!--Excerpt-->
<p> <?php the_excerpt(); ?> </p>
</div>
<?php
//loop ends
endwhile; endif; wp_reset_postdata();
?>
Place this file on your non wordpress site where your index file is and include the above latest.php where you want.
<?php include_once("latest-post.php"); ?>
If you are using HTML file, The PHP will not exicute. You can either rename index file .html to .php or add
AddType application/x-httpd-php .html
to your .htaccess. Take a look at
Run PHP from HTML
Change the number "showposts=5" to how many posts you want, and change "cat=blogs" to "cat=your category name"
Here is an example what I am using on my site to read the headings from the database:
<?php
//Database access
$dbname="db-blog";
$dbhost="localhost";
$dbuser="user";
$dbpass="secretpassword";
//SQL Befehl zur Abfrage der Postings
$sql = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER by ID DESC LIMIT 0,6";
//Open database
$db = mysql_connect($dbhost,$dbuser,$dbpass) or die("no connection to database");
mysql_select_db($dbname, $db);
$sqlRes = mysql_query($sql,$db);
mysql_close($db);
//Output titles
$recordCount = mysql_num_rows($sqlRes);
for ($i = 0;$i < $recordCount;$i++) {
$arCur = mysql_fetch_array($sqlRes);
echo "<li>" . $arCur["post_title"] . "</li>";
}
?>
Should be easy to adapt to also read the contents if needed.
If you dont have direct access to the database, the following script accesses the blog items from the RSS feed.
<?php
//Settings
$blog_url = "http://blog.ekiwi.de"; //URL of blog without / at the end
$count = 5; //Number of posts that should be shown
//--------------------------------------------------------------------------------
$content = #file_get_contents($blog_url . "/?feed=rss2");
preg_match_all("/<item[ ]?.*>(.*)<\/item>/Uis", $content, $items);
$items = $items[1];
if(!empty($items)) {
if ($count > sizeof($items))
$count = sizeof($items);
for($i = 0; $i < $count; $i++) {
//read link
preg_match("/<link>(.*)<\/link>/Uis", $items[$i], $link);
//Read title
preg_match("/<title>(.*)<\/title>/Uis", $items[$i], $array_title);
$title = str_replace("<![CDATA[", "", $array_title[1]);
$title = str_replace("]]>", "", $title);
//Output title with link
echo "<li>\n";
echo " $title\n";
echo "</li>\n";
}
}
?>
Both solutions use PHP.