adding multiple do statements on a page using php and mysql - php

Ok so I didn't see anything like this in the previous postings so if if has been covered before sorry for the repost.
I am trying to put a second do..while statement in the same page but when I do this it seems to drag down the page. it loads fine if I have only one statement in there but the minute I put in a second one it takes forever to load the page. I have a need of adding a total of 9 do..while statements which will most likely kill the page at this point. I'm not sure why it would be doing this any ideas?
The code I'm using is as follows
<p><?php
do {
$row_myContacts = mysql_fetch_assoc($myContacts);
if ($row_myContacts['staffID'] != "81"){
}
else {
echo $row_myContacts['firstName'] . " " . $row_myContacts['lastName'];?></p>
<p class="staff_title_national"><?php echo $row_nmyContacts['titleName'];?></p>
<br />
<li><?php echo $row_myContacts['address1']
. " " . $row_myContacts['address2'] ;?></li>
<li><?php echo $row_myContacts['cityName']
. ", " . $row_myContacts['state_abreviation'] . " "
. $row_myContacts['zipCode'];?></li>
<br />
<li>Office: <?php echo format_phone($new_office_phone); ?></li>
<li>Fax: <?php echo format_phone($new_fax_phone); ?></li>
<br />
<br />
<li>E-Mail:
<?php echo $row_myContacts['email'];?></li>
<br />
<?php }
} while ($row_myContacts['staffID'] != '81')?>
I have tried testing it so that the staffID is == to the number i need, but it doesn't output anything. I think it should be that way but for some reason it doesn't work. and yes I moved the stuff in that variation so that the else was empty and the if had the information in it.
The page holds 4 sections of contacts under section 1 there is one section with 2 names (same address info) section 2 has 3 names different info, sections 3 and 4 have 2 each with different info. I even created 2 new tables in my database that reference the 4 sections if that helps. I do know my sql statement is correct it pulls all 9 contacts with their corresponding information.
/edit below/
so I also tried the code this way. and I do get it to load the first one but when I add a section for loop it shows nothing. I think it is because I am telling the loop to access the database at the same point that it left off so it isn't starting over on the loop.
<?php
for ($x=$totalRows_myContacts; $x>=1; $x--){
if ($row_myContacts['staffID'] == '81') {
echo $row_myContacts['firstName'] . " " . $row_myContacts['lastName'];?></p>
<p class="staff_title"><?php echo $row_myContacts['titleName'];?></p>
<br />
<li><?php echo $row_myContacts['address1'] . " " . $row_myContacts['address2'] ;?></li>
<li><?php echo $row_myContacts['cityName'] . ", " . $row_myContacts['state_abreviation'] . " " . $row_myContacts['zipCode'];?></li>
<br />
<li>Office: <?php echo format_phone($new_office_phone); ?></li>
<li>Fax: <?php echo format_phone($new_fax_phone); ?></li>
<br />
<br />
<li class="emailTealGrey">E-Mail: <?php echo $row_myContacts['email'];?></li>
<br />
<?php }$row_myContacts = mysql_fetch_assoc($Contacts);
}?>
so not sure how to tell the database to access itself again and start the query from the top I guess is what I need to do. any ideas how to tell it to do that?

What happens if there is no row for staffID=81
change code to something like this,
.....
do {
$row_myContacts = mysql_fetch_assoc($myContacts);
if ($row_myContacts == false){
break;
}
.....

Related

How to loop through rows for data to display multiple results?

I have searched many forums including Stack Overflow and not been able to find a solution for this that works. Please do not mark this as duplicate.
I have a database of users with multiple entries for certain items. The users are saved in one table ("TABLE 1") and the items are saved in another ("TABLE 2"). What I need the code to do is search TABLE 2 for user IDs that match the selected user from TABLE 1 and then for each matching entry display the item data for that row. The code I currently have is below but it is only displaying one result and then stopping. Any help is greatly appreciated.
<?php
$queryItems = mysql_query("SELECT * FROM items WHERE user_id='$results[id]'");
$itemmatch_result = mysql_fetch_array($queryitems) or die($itemmatch."<br/><br/>".mysql_error());
{
?>
<div class="column6">
<div class="item-container">
<p class="itemname"><?php echo $itemmatch_result['item_make'] . " BRAND " . $itemmatch_result['item_type']; ?> item</p>
<p class="itemsize"><strong>Item Size:</strong> <span><?php echo $itemmatch_result['item_size']; ?></span></p>
<p class="tiresize"><strong>Item Model:</strong> <span><?php echo $itemmatch_result['item_model']; ?></span></p>
<p class="tiresize"><strong>Registered:</strong> <span><?php echo date('F jS, Y', $itemmatch_result['item_registered']); ?> AT STORE</span></p>
<span class="purchase-data">BOUGHT <?php echo date('F jS, Y', $itemmatch_result['item_bought']); ?></span>
</div>
<?php
}
unset($itemmatch_result);
?>
You need to loop through all records as long as the query is giving you the records you expect.
while ($data = mysql_fetch_array($queryItems)) {
echo '
<p class.........>' . $data['item_make'] . ' BRAND .....
';
}
Btw, you shouldn't be using mysql anymore as it's deprecated, I would suggest starting to use mysqli/pdo instead.

simple php pagination or css pagination with php/mysql

I am trying to built a simple php pagination ( a sort of ).
or to be more specific css pagination with php/mysql.
Accessing and storing the values from db
while($row = $result->fetch_assoc()) {
$id[]=$row["id"];
$name[]=$row["name"];
$url[]=$row["url"];
//echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["url"]. "<br>";
}
Displaying them
<ul class="pagination">
<li>«</li>
<?php
$i=1;
for ($x = 0; $x < count($id); $x++) { ?>
<li>
<a href=<?php echo "$url[$x]"; ?> target="iframe1"
title="<?php echo "$name[$x]"; ?>"><?php echo "$i";?></a>
</li>
<?php $i++;} ?>
<li>»</li>
</ul>
Once any number shown above is clicked required url is displayed inside iframe1
<div align="center">
<iframe name="iframe1" src="http://www.w3schools.com"
frameborder='0' height='1000' width='1000' align="center"></iframe>
</div>
when I click any number it display the url inside the iframe "iframe1" .
Question:
How do i display first and NEXT {NOT LAST} record in
<li>«</li> [First]
<li>»</li> [Next]
I am looking for a very small code to do the same.
The first link is pretty easy:
<a href=<?php echo "$url[0]"; ?> target="iframe1"
title="<?php echo $name[0]"; ?>"><<</a>
For next link It would kind of depend on how your urls are set out. If the $url values are just a name and your url is domain.com/blog?page=page1 where page1 is the value from $url then we get use $_GET['page'] to grab the current page. Then using:
$next = array_search($_GET['page'], $url);
will give us the number in the order of the current page so the next page will simply be $url[$next + 1]
EDIT:
OK if its a url that changes in domain part then the $_GET wont work. You could grab the current url using $_SERVER[REQUEST_URI] and then use the same technique as above to find its position in the array of urls.

Link PHP inside an echo tag

I am having a problem with this code
<?php
echo '<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|VIEW AD</b></span>';
}
}?>
for some reason when the VIEW AD link is clicked it doesn't build it properly and still contains the php code in the link rather than the link to the actual ad page. is it an issue with an echo in an echo ?
I'm sure this isn't quite difficult to solve but I have been trying for far to long on my own and cant get it.
Thanks, any help would be great.
You actually had it right in the first part of your string. You can't have and echo statement inside of another echo statement. Use concatenation throughout your string:
<a href="' . $adurl . '"
You have two extra brackets at the end and php text inside your echo.
<?php
echo '
<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b>
</div>
<br />
<span class="orange">
<b>
HOME | VIEW AD
</b>
</span>';
?>
All fixed given that $adurl is defined.
This
<?php echo $adurl; ?>
Should be
' . $adurl . '
i.e.
echo '<div class="post_note2"><b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|<a href="'.$adurl.'>VIEW AD</a></b></span>';

Random banner display using php and mysql

I'm creating my website located here
I'm trying to add a banner that changes every time a page refreshes. I have set up 2 examples in my database called "link 1" and "link 2". I will want to add more as and when I get them.
What I want to happen is this:
I want to display one of the 2 images on my site and when the user refreshes the page, it will select one of the 2 images and this should continue every time the page refreshes.
I'm testing this out in a page called banner.php before I move it to my footer.php and make it live.
I currently have this code In my banner.php page:
<?PHP
include_once('include/connection.php');
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
global $pdo;
$query = $pdo->prepare ("SELECT * FROM banners ORDER BY RAND() LIMIT $num_displayed");
$query->execute();
// For all the rows that you selected
while ($row = execute($result))
{
// Display them to the screen...
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
<br /><br /><br />
But I am getting this error code:
Fatal error: Call to undefined function execute() in banner.php on line 13
My connection page is used by other pages so I know it works.
Please can some one help me on what I am doing wrong.
Need any more info then please ask and I will add it to this post.
Thank you.
Kev
replace this
while ($row = execute($result))
with this:
while ($row = $query->fetch())
EDIT
This make it better to read.
while ($row = $query->fetch()) :
// Display them to the screen...
?>
<a href="<?php echo $row['link']; ?>">
<img src="<?php echo $row['image']; ?>" border="0" alt="<?php echo $row['text'];?>">
</a>
<?php endwhile; ?>
<br/>
<br/>
<br/>

Making a content "feed" with mysql database and php, is this a safe way?

I'm making a a side bar feed that will display the 10 most current things submitted into my database. I'm very new to all of this, so I am wondering.. is this an ok way of going about doing it? it works.. not automatically but when i submit something into my database, it goes there.. i submit something else and then the top goes to to the second..! i just cant shake the feeling that maybe this isnt a good way to do it.
the top 3 sections
<div class="span3 offset3">
<?php include 'feed/one.php'; ?>
<ul class="nav nav-list well">
<li class="nav-header"></li>
<li class="active">HIT INFO</li>
<?php while($row = $data->fetch_assoc()) { ?>
<li><a href="<?php print $row['link']?>"><?php
Print "<tr>";
Print "<th>Hit:</th> <td>".$row['hit'] . "</td> ";
Print "<th>Amount:</th> <td>".$row['amount'] . " </td>";
Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
Print "<br><br/>";
Print "</table>";?></a></li>
<?php } ?>
<li class="divider"></li>
<?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 1, 1"); ?>
<?php while($row = $data->fetch_assoc()) { ?>
<li><a href="<?php print $row['link']?>"><?php
Print "<tr>";
Print "<th>Hit:</th> <td>".$row['hit'] . "</td> ";
Print "<th>Amount:</th> <td>".$row['amount'] . " </td>";
Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
Print "<br><br/>";
Print "</table>";?></a></li>
<?php } ?>
<li class="divider"></li>
<?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 2, 1"); ?>
<?php while($row = $data->fetch_assoc()) { ?>
<li><a href="<?php print $row['link']?>"><?php
Print "<tr>";
Print "<th>Hit:</th> <td>".$row['hit'] . "</td> ";
Print "<th>Amount:</th> <td>".$row['amount'] . " </td>";
Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
Print "<br><br/>";
Print "</table>";?></a></li>
<?php } ?>
<li class="divider"></li>
<?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 3, 1"); ?>
<?php while($row = $data->fetch_assoc()) { ?>
<li><a href="<?php print $row['link']?>"><?php
Print "<tr>";
Print "<th>Hit:</th> <td>".$row['hit'] . "</td> ";
Print "<th>Amount:</th> <td>".$row['amount'] . " </td>";
Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
Print "<br><br/>";
Print "</table>"; ?></a></li>
<?php } ?>
the data that gets submitted gets displayed on my main page until something else gets submitted, goes to the "feed", and also goes to another page that shows the past data.
You don't appear to have anything the user can intercept, so "safe" is a non issue.
You are querying illogically though. There's no reason to repeat the same query with multiple offsets all over the page when you can just fetch the data initially and use that object to relay the content where needed. Run a query like this at the beginning of the file, or ideally before any output is rendered.
"SELECT * FROM hit ORDER BY hit_id DESC LIMIT 4"
Now you have 4 items, so accessing those throughout the page could be something like this:
<ul class="nav nav-list well">
<li class="nav-header"></li>
<li class="active">HIT INFO</li>
<?php while($row = $data->fetch_assoc()): ?>
<li>
<a href="<?php print $row['link']?>">
<table>
<tr>
<th>Hit:</th> <td><?php echo $row['hit']; ?></td>
<th>Amount:</th> <td><?php echo $row['amount']; ?></td>
<th>Category:</th> <td><?php echo $row['category']; ?></td>
</tr>
<br><br/>
</table>
</a>
</li>
<li class="divider"></li>
<?php endwhile; ?>
</ul>
Now, you have a few markup errors there. You close a table you never open. This can only in a small way be considered tabular data, so tables are probably not the best way to do this anyhow. I myself don't understand your use of th and td in this way, but if it works for you then good.
You should break out of php to display html. echoing something like "<th>" is totally unnecessary.
Instead of each time querying the database and getting a record to show it, you should do it in a single query which fetches the number of rows you need:
<?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 1, N"); ?>
This fetches the first N rows, where N has to be numeric of course. Then you should loop throw the rows and print it just the way you are doing now. The only difference is that it might take you a while to get the nuances of opening/looping/closing tables correctly, but it is a really good investment of your time to wrap your head around it!
You're actually using the hit_id field to order the data you get from the database.
If you want actually to retrieve the "10 most current things submitted into my database", you'll need something else than an auto-incremented ID. (Something like a COUNT on the amount row). It will depends on the actual value / way to fill this field but it might look like this instead :
SELECT * FROM hit GROUP BY Category ORDER BY amount DESC;
Concerning this part :
but when i submit something into my database, it goes there.. i
submit something else and then the top goes to to the second
The reason is quite simple, you're using ORDER BY hit_id DESC, that's why the last inserted ID is coming first.

Categories