how can i change this mysql code to pdo? - mysql

i have this code in iclude for connect to database
i have in this file mysql connection function too but i decide change mysql to pdo
-include.php:
try{
$conn = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
return $conn;
}
catch(PDOException $e)
{
echo 'can not connect to database';
exit();
}
i have mysql query in index.php but i want change this code to pdo
<?php
$sql=mysql_query("SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18");
if(mysql_num_rows($sql)) {
$j=0;
while($result = mysql_fetch_object($sql)) {
$j++;
if($result->id == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
}
}
?>
how can i change mysql query in this code to pdo?

Read some tutorial (there are plenty even here on SO)
Run some simple queries to make yourself familiar with PDO
Start with rewriting.

You should read some doc about PDO and do some tutorial. But to help you, here is a really simple sample to begin with PDO :
<?php
$connexion = new PDO("mysql:host=$PARAM_hote;dbname=$PARAM_db_name", $PARAM_user, $PARAM_pwd); // DB connexion
$results=$connection->query("SELECT member FROM member ORDER BY member ASC"); // retrieving all entries from member table
$results->setFetchMode(PDO::FETCH_OBJ); // retrieving results as objects
while( $lines= $results->fetch() ) // retrieving list of member
{
echo 'user: '.$lines->member.'<br />'; // displaying members
}
$results->closeCursor(); // close results cursor
?>
And here a more sophisticated one with params (prepared query) :
<?php
// connection opening ...
$prepared_query=$connecion->prepare("SELECT id FROM member WHERE member_id= :id"); // query prepare
$prepared_query->execute(array( 'id' => 1 ));
$lines=$prepared_query->fetch(PDO::FETCH_OBJ);
echo $lines->id.'<br />';
?>
It's an old sample of mine I hope will help you.

Something like this:
<?php
$sql = "SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18 ";
$sth = $conn->query($sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);
if ($count = $sth->rowCount() > 0){
$j=0;
while ($row = $sth->fetch()) {
$j++;
if($row['id'] == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
}
}
?>

Related

How to display out of stock status for both simple product & configurable product in Product page magento1.9

I have magento website that develop using 1.9 version and i need to display the out of stock status on product page for both simple product & configurable product
I have tried below method and It's only getting status of simple products
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$qty = $stock->getQty();
What i need is How to display the out of stock status for simple and configurable products in product page
This code works for me,
Hope this will help you.
if( $_product->getTypeId() == 'simple' ){
if ($_product->getStockItem()->getIsInStock()<= 0) { ?>
<span class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></span>
<?php }else { ?>
<span class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></span>
<?php };
}
else{
$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
$stockTotal = $stockTotal + $stock;
}
if ($stockTotal <= 0){ ?>
<span class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></span>
<?php } else{ ?>
<span class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></span>
<?php }
}

count up the rows in acf (repeater-field) and add html output for every row

I want to output a list element () for each row of an ACF repeater field. As soon as a new row is created, a new list element shoul be created with the value of the counter (cout up).
here is the code. the list elements should cout up according to the number of rows:
HTML:
<ul>
<li data-id="1" class="active">1</li>
<li data-id="2">2</li>
<li data-id="3">3</li>
<li data-id="4">4</li>
<li data-id="5">5</li>
<li data-id="6">6</li>
<li data-id="7">7</li>
<li data-id="...">...</li>
</ul>
PHP (ACF):
<?php
$i = 1;
if (have_rows('referenz-slide')) :
$counter = 0;
while (have_rows('referenz-slide')) : the_row();
$counter++;
// vars
$title = get_sub_field('title');
$link = get_sub_field('link');
$text = get_sub_field('text');
?>
<?php $state = "";
if ($i == 1) {
$state = "active";
} else {
$state = "hidden";
} ?>
<div class="referenz-content <?php echo $state; ?>" id="ref-<?php echo $i; ?>" data-referenz="<?php echo $i; ?>">
<h4 class="referenz-headline"><?php echo $title; ?></h4>
<p><?php echo $text; ?></p>
Jetzt mehr erfahren
</div>
<?php $i++;
endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I would like to increment the list items and the value of the data id (html output) according to the number of ACF rows. Can someone help?
So you pretty much have it all there already
<ul>
<?php while (have_rows('referenz-slide')) : the_row();?>
<li data-id="<?php echo $i;?>" class="<?php echo $state; ?>">
<?php echo $i;?>
</li>
<?php $i++; ?>
<?php endwhile; ?>
</ul>

How do I echo the each posts category in wordpress

I am looking to make wordpress print/echo the category name of a post into it's class. I need this to work on the index page inside the main loop. Here is what I mean:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="<?php ** I NEED THIS CODE ** ?>">
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else {} ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
Hopefully you understand my poor description of my issue. Thanks
From http://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts/
Add the following to your theme's functions.php file:
function the_category_unlinked($separator = ' ') {
$categories = (array) get_the_category();
$thelist = '';
foreach($categories as $category) { // concate
$thelist .= $separator . $category->category_nicename;
}
echo $thelist;
}
And the corresponding markup would be:
<article class="<?php the_category_unlinked(' '); ?>">

PHP mysql Query Bug

I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.

How can I get the row of a certain link in a table with data from a mysql database?

Here is my code:
<html>
<?php
DEFINE('DATABASE_USER', 'sfasdfasd');
DEFINE('DATABASE_PASSWORD', 'asdfasdfasdf');
DEFINE('DATABASE_HOST', 'sdfasdfasd');
DEFINE('DATABASE_NAME', 'dsafsdfasd');
$connect = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME) or
die ("Hey loser, check your server connection.");
mysql_select_db("minedb");
$query = "SELECT * FROM ideastable ORDER BY datee DESC";
$quey1="select * from ideastable";
$result=mysql_query($query) or die(mysql_error());
?>
<table border=1 style="background-color:#000000;" >
<caption><EM>Ideas List</EM></caption>
<tr>
<th>IDEAS</th>
<th>Thumbs Ups</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['idea'];
echo "</td><td>";
echo $row['thumbsup'];
$i = $row['id'];
echo $i;
echo 'Thumbs Up!';
echo "</td></tr>";
}
echo "</table>";
?>
<SCRIPT type="text/javascript" src="jquery.min.js"></SCRIPT>
<SCRIPT type="text/javascript">
function doSomething() {
var myVar = "<?php echo $i; ?>";
alert(myVar);
$.load('uts.php?i=myVar');
return false;
}
</SCRIPT>
</html>
My question is how would I be able to make it so that when I click the Thumbs Up it recognizes the row the link was in? I am making a site where you can rate some of the objects in a database and that is the start up of it.
How about passing id to javascript function?
echo 'Thumbs Up!';