I use PHP and ODBC, and I was wondering how to make if condition for an exact number...?
$DB = $core_db->Execute("SELECT TOP (10) ID, Name, Age, Location FROM Profiles ORDER BY Name");
$num=0;
while(!$DB->EOF){
if ($DB->fields[0] != 0) {
echo'<div class="ID">'.++$num.'</div>
<div class="name">'.$DB->fields[1].'</div>
<div class="name">'.$DB->fields[2].'</div>
<div class="name">'.$DB->fields[3].'</div>
';
}
$DB->MoveNext();
Now I want to use if with echo'<div class="ID">'.++$num.'</div> to stylize first 3 profile but I don't know how!
I tried
if ($num === 1){
echo'<div class="ID">1*</div>';
} else {
echo'<div class="ID">'.++$num.'</div>';
}
and it didn't work with me, it gave 1* to all rows not first one only.
$DB = $core_db->Execute("SELECT TOP (10) ID, Name, Age, Location FROM Profiles
ORDER BY Name");
$num=0;
while(!$DB->EOF){
if ($DB->fields[0] != 0) {
echo'<div class="ID">'.$num.'</div>
<div class="name">'.$DB->fields[1].'</div>
<div class="name">'.$DB->fields[2].'</div>
<div class="name">'.$DB->fields[3].'</div>
';
$num++;
}
$DB->MoveNext();
try this, here I'm incrementing your $num when it runs from each loop.
here I'm doing post-increment,
Keep it simple
if ($num == 1){
echo'<div class="ID">1*</div>';
$num = $num + 1;
} else {
echo'<div class="ID">'.$num.'</div>';
$num = $num + 1;
}
Related
I need to divided all data into 4 columns. i have used this method but its not coming column property
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
$tempcount = 1;
if ($stmt1->execute(array())) {
while ($row = $stmt1->fetch()) {
?>
<?php if ($tempcount == $pages) { ?>
<div class="column">
<div class="ui bulleted list">
<?php } ?>
<?php
if ($tempcount == $pages) {
$pages = $pages + $pages; ?>
</div>
</div>
<?php }
$tempcount++;
}
} ?>
how i can get this columns after getting count of data i have and add those divs in between.
for an Example 20 data rows 20/4 = 5 after each 5 data, i need start and end to be added
<div class="column">
<div class="ui bulleted list">
</div>
</div>
<div class="column">
<div class="ui bulleted list">
</div>
</div>
// And so on
result will be like this
thank you very much
Here's a proper code with comments
$tempcount = 0;
$pages = ceil($rowcount / 4);
while ($row = $stmt1->fetch()) {
// if result of % (Modulo) is zero - you need to start new column
if ($tempcount % $pages == 0) {?>
<div class="column">
<div class="ui bulleted list">
<?php
}?>
ITEM
<?php
// if result of % (Modulo) is (pages - 1) - you need to close previous column
if ($tempcount % $pages == ($pages - 1)) {?>
</div>
</div>
<?php
}
$tempcount++;
}
// check if you have to close previous column
// because it was not closed in a while loop
if (0 < $rowcount) {
if ($tempcount % $pages != 0) {?>
</div>
</div>
<?php
}
}
Whilst the modulo approach mentioned above will help in these situations, I often find that writing your loops from a different perspective can help in sorting out the situation, and making things more readable.
Rather than looping through every row, and then inserting the columns where you think they need to be. Instead loop every item, but in a controlled count, wrapped by the columns.
I've switched the code to using echo just due to personal preference, you can still use php-breakouts instead if preferred.
Please note this is untested example code, just to illustrate the point:
<?php
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
if ($rowcount) {
$stmt1->execute(array());
do {
$group = '';
$group .= '<div class="column">';
$group .= '<div class="ui bulleted list">';
for ( $i=0; $i<$pages; $i++ ) {
$row = $stmt1->fetch(); if ( !$row ) { break; }
// presumably something would be done with $row here
$group .= '';
}
$group .= '</div>';
$group .= '</div>';
echo $group;
} while ( $row );
}
% modulo will help you PHP operators. While you fetch, use it to create your divider. If using div and if needed, I usually add a tiny div with clear: both to make sure it breaks the line after the 4/8/12... results.
$cell++;
$jumpline = ($cell % 4) ? "" : "<div class=\"spacer\"></div><br />";
$endit = ($cell % 4) ? "" : "</div>";
I am having a rather strange issue and for the likes of me, cannot figure it out!
Basically, I have users who are allowed to upload documents, which are then associated with their profile.
If the user decides to delete a document, the only thing that gets deleted here, is the document, but not the content included, e.g. comments, title, etc - it's as if nothing ever happened - except of course - the physical document has been deleted - sql entries however have not.
mydocs.php:
if ($_SESSION['USERID'] != "" && $_SESSION['USERID'] >= 0 && is_numeric($_SESSION['USERID']))
{
if($_REQUEST['submitdelete']!="")
{
$deletedoc = $_POST['deletedoc'];
$svcount = count($deletedoc);
for ($i = 0; $i < $svcount; $i++)
{
if ($deletedoc[$i] != "" && $deletedoc[$i] >= 0 && is_numeric($deletedoc[$i]))
{
$query = "SELECT * FROM docs WHERE DID='".mysql_real_escape_string($deletedoc[$i])."'";
$executequery = $conn->execute($query);
$theuserid = $executequery->fields['USERID'];
$doc_name = $executequery->fields['doc_name'];
if(mysql_affected_rows()>=1)
{
$docpath = $config['docdir']."/".$doc_name;
#chmod($docpath, 0777);
if (file_exists($docpath))
{
#unlink($docpath);
}
if($theuserid == $_SESSION['USERID'])
{
$deletefrom[] = "docs";
$deletefrom[] = "docs_comments";
$deletefrom[] = "docs_favorited";
for($j=0;$j < count($deletefrom);$j++)
{
$query = "DELETE FROM ".$deletefrom[$j]." WHERE DID='$deletedoc[$i]'";
$conn->Execute($query);
}
$tempthumbs = $config['thumbdir']."/".$deletedoc[$i].".jpg";
if(file_exists($tempthumbs))
{
#unlink($tempthumbs);
}
if ($svcount > 1)
{
$message = $lang['643'];
}
else
{
$message = $lang['644'];
}
}
else
{
if ($svcount > 1)
{
$error = $lang['645'];
}
else
{
$error = $lang['646'];
}
}
}
}
}
}
mydocs.tpl:
<form id="deleteform" name="deleteform" action="{$baseurl}/mydocs.php" method="post">
{section name=i loop=$docs}
{insert name=seo_clean_titles assign=title value=a title=$docs[i].title}
<div class="column {if $smarty.section.i.iteration % 6 == 0}last{/if}">
<div class="image"><img src="{$vthumburl}/{$docs[i].doc_name|truncate:-4:"":true}.jpg" alt="{$docs[i].title|stripslashes|truncate:25:"...":true}" ></div>
<h3>{$docs[i].title|stripslashes|truncate:17:"...":true}
<br />{$lang485}: <input type="checkbox" name="deletedoc[]" value="{$docs[i].DID}">
<br />{$lang318}</h3>
</div>
{/section} <div class="btndelete">
<input type="submit" value=" " name="submitdelete"></div>
</form>
Urgently awaiting a solution / assistance.
Many thanks in advance!
There is nothing wrong with the code.
For some reason, the connect.php was using write only permissions to the sql db.
Change it to All Privileges and now it works.
Now to secure it.
I am kinda lost with what I am trying to achieve here, really need some help with the following.
While fetching DB using script provided below, I need to add <div class="clear"></div> after every third occurrence and if it is less than 3, lets say 2 or even one and there is no more after that after that last one.
Here is my script
<?
$template_query = 'SELECT * FROM Files WHERE parentpageID = :id and show_in_category = "1" ORDER BY ID asc';
$res = $db->prepare($template_query);
$res->execute(array(':id' => $current));
$add_rowNum = 0;
while ($info = $res -> fetch()){
$add_rowNum++;
$templateTitle = $info['templateTitle'];
$add_refering_url = $info['referring_url'];
$templ_link = $category_folder.$add_refering_url;
$teaserText = $info['teaserText'];
$path_to_add_images = $image_path.$info['ImagePath'].DS;
$add_img_info = $path_to_add_images.$info['templateImage'];
$add_img_alt && $add_img_title && $templateTitle = $info['templateTitle'];
list($width, $height, $type, $attr) = getimagesize($add_img_info);
$last_class = ($add_rowNum == $res->rowCount()) ? 'frame' : 'frame frame_margin';
print<<<END
<div class="$last_class">
<div class="prod">
<div class="title"><a href="$templ_link">$templateTitle
<img src="$add_img_info" alt="$add_img_alt" $attr title="$add_img_title"></a>
</div>
</div>
<div class="prd">
$teaserText
</div>
</div>
END;
}
?>
your help is highly appreciated
Use below code:
If(($add_rowNum % 3) == 0) {
echo "<div class='clear'></div>";
}
Add this code after the END syntax.
Using with %
if ($add_rowNum % 3 == 1)
echo '<div class="clear"></div>';
Try this. I think this logic will solve this problem!
$total_records = 10;
for($i=1;$i<=$total_records;$i++) {
echo "Hi";
if($i%3 == 0) {
echo "<br/>==================<br/>";
}
if(($i == $total_records) && ($total_records%3 != 0)) {
echo "<br/>==================<br/>";
}
}
I'm trying to make a virtual shelf type of thing which is populated via a MySQL database. The column shelfPos holds the position of the item on the shelf. Each row/'shelf' starts with <div class="shelfRow"> and obviously ends with </div> so it's styled and positioned correctly. Items on the shelves can be moved around using the jQuery UI droppable interaction.
The overall layout is this: http://jsfiddle.net/aRA5D/
Each shelf can hold 5 items (left to right).
I'm having trouble populating the shelves. At the moment I've got this: (This is in the place of the HTML)
<?php
$sql="SELECT * FROM shelf WHERE userID='$userID'";
$result=mysql_query($sql);
if (mysql_num_rows($result) == 0) {
// Show a message of some sort? (No items)
}
else {
$tries = 1;
$times = 10; // How many shelves. (10 = 2 shelves)
while(($row = mysql_fetch_array($result)) && ($tries <= $times)) {
while ($tries <= $times) {
if ($tries == $row['shelfPos']) {
echo '<div class="drop" id="drop'.$tries.'"><div class="boxArt" id="'.$row['gameID'].'">'.$row['gameID'].'</div></div>';
}
else {
echo '<div class="drop" id="drop'.$tries.'"></div>';
}
$tries = $tries + 1;
}
$times = $times + 5;
}
}
?>
There's several things wrong with it. It doesn't include the <div class="shelfRow"> html (didn't know how/where to put it, as it needs to be echoed after every 5 'blank' and real items - for loop maybe?) and it requires me to input the number of shelves (2 in this case). Would it be possible to determine how many shelves are required based on the item's position? It's awkward to do because it also needs to echo 'blank' .drop divs before and after them so that the items can be moved around.
Hope this all makes sense. Thanks for the help!
First u need to get data in order of ShelfPos
"SELECT * FROM shelf WHERE userID='$userID' order by shelfPos asc"
And try this code:
...
$i = 0;
while($row = mysql_fetch_array($result)) {
//Each 5
if($i % 5 == 0) echo '<div class="shelfRow">';
if ($i == $row['shelfPos']) {
echo '<div class="drop" id="drop'.$i.'"><div class="boxArt" id="'.$row['gameID'].'">'.$row['gameID'].'</div></div>';
}
else {
echo '<div class="drop" id="drop'.$i.'"></div>';
}
//close shelfrow div
if($i % 5 == 4) echo '</div>';
$i++;
}
//to complete the loop
$shelv_left = 5 - ($i % 5);
if($shelv_left < 5) {
for($j=0; $j < $shelv_left; $j++) {
echo '<div class="drop" id="drop'.($i+$j).'"></div>';
}
echo '</div>'; // end shelfrow div
}
...
i am trying to get my data from the database and wrap it with html tags. here is the working code so far:
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
Here I am getting everything from the database (Select * from portfolio), but in the portfolio I have, websites, demos and graphics; so I wanted to get only the data where category = "web" from the above code, so I tried this:
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
if($row->category = "web"){
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
}
now the nested if statements do not generate the divs I need, how can I get this working
thanks for your help
I can't see your SQL based on your question, but you could just modify your SELECT query to include WHERE category="web"
This way, you're only selecting the rows you need, instead of looping over every row in that table.
Additionally, it appears that you're using assignment = instead of comparison == for your if statement.
Do you just need to have == instead of =?
if($row->category == "web"){
But it would be best to restrict the query to the results you need at the database level, unless you need the other rows for some reason.
1)You missed an equal sign:
if($row->category = "web") => if($row->category == "web")
Or better yet
if($row->category === "web")
2)If you want to only get fields with a specific category field, you can simply change your query:
[rest of your query] WHERE category="web"
OK, it should go like this, assuming the fields are sorted as follows
ID, category, website, thumb2, demo, graphics
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
if($row[1] == "web"){
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row[3] . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
}
and there is no need for the nested if, you can just use it in one line as follows:
if($row[1] = "web")
{
echo '<div class="gal1">';
echo ' <div class="gal"><img src="img/' . $row[3] . '.jpg"></div>';
echo '</div> <!-- gal1 -->';
}
1) Change your query to contain a WHERE category="web" clause
2) You have an assignment operator in your if clause (=), when you need an equality operator (==)