how to categories text or code format from a given string - php

I'm working university group project displaying code and text in a different view. I want to identify codes and text from a given string. if in a given string text and code is occurring.
I tried this code
e.g
$lists="text format detail.
$query='SELECT * from comment where QueID='yuy';
$listfile=mysqli_query($conn,$query);
$count=mysqli_num_rows($listfile);
more detail about above code..";
$arr = explode(".", $lists);
?>
<div class="code">
<?php for ($i=0; $i < count($arr); $i++)
{
if (preg_match("/[a-zA-Z.',]/",$arr[$i]))
{?>
<div><?php echo $arr[$i]?></div>
<?php }
else
{?>
<pre><code><?php echo $arr[$i]?></code></pre>
<?php } ?>
}?>
thank you for the answer

Related

PHP Output my XML feed in a random order

I have my XML output working fine but it's in latest date order. What would be cool is if I could get it in a random order. I've tried using shuffle and I just can't get it working (nothing seems to happen).
Here's an example of the code I'm using to build my XML. The output is HTML with PHP to insert variables.
// Build the Feed
$feed = "http://www.reviewswebsite.com/api/consumer-reviews/?username=" . $userName . "&format=xml&reviews_per_page=20&page=" . $pageNumber;
$xml = simplexml_load_file($feed);
for($i = 0; $i < $numberOfForLoops; $i++)
{
$reviewer_name = $xml->reviews->review[$i]->reviewer_name;
$date_of_work = date('l, d m Y' , strtotime($xml->reviews->review[$i]->date_of_work));
$average_reviewer_rating = (float)$xml->reviews->review[$i]->average_rating;
..
?>
<div><?php echo $reviewer_name; ?></div>
...
<?php ;} ?>
#The Fourth Bird helped me by passing this link: Randomize SimpleXML object results
The end result I created for Trust-A-Trader reviews which I built into a Joomla! module was. I'll try to release the module for free on the Joomla! Extensions Directory (JED).
End code was
$feed = "http://www.trustatrader.com/api/consumer-reviews/?username=" . $userName . "&format=xml&reviews_per_page=20&page=" . $pageNumber;
$xml = simplexml_load_file($feed);
// Count Reviews, if total number of reviews is less than the total shown on page set in module then show the lesser amount to avoid an error.
$totalNumberOfReviews = count($xml->reviews->review);
$numberOfForLoops = $totalNumberOfReviews;
if ($totalNumberOfReviews > $numberOfReviews) {
$numberOfForLoops = $numberOfReviews;
}
// If the module sets the order to random then shuffle array, or else do in latest first date order.
if ($params->get('Order') == 0) {
foreach($xml->reviews->review as $val)
$array[]= $val;
shuffle($array);
}
// Loop through the reviews and output them.
foreach($array as $val)
{
$i = 0;
if ($i < $numberOfForLoops)
{
?>
<div class="mod_trust_a_trader_reviews--review g-grid" itemscope itemtype="http://schema.org/Review" itemprop="review" >
<div class="review g-block size-100" itemprop="reviewBody">
<?php echo $val->comments; ?>
</div>
<div class="authorBlock g-grid size-100">
<span class="author g-block size-100" itemprop="author">
<?php echo $val->reviewer_name; ?>
</span>
</div>
<div class="score g-block size-100 g-grid">
<div class="g-block size-100">
<?php starRatingImage($val->average_rating); ?>
</div>
<div class="g-block size-100 reviewRating">
<?php echo '(<span itemprop="reviewRating">' . $val->average_rating . '</span>)'; ?>
</div>
</div>
</div>
<?php
}
$i++;
}
?>

How to split php output in multiple columns?

I'm new to php and I've searched for solutions to my problem through all the internet but still cant find a solution for my problem.
I have a php code that looks like this:
<?php
if ($comments_count) {
for($i=0; $i<$comments_count; $i++) {
if ($dont_show_email[$i] != '1' && $email != '') {
$author[$i] = "<a href=\"mailto:{$email[$i]}\">{$author[$i]
}
</a>";
}
$text[$i] = str_replace(chr(13), '<br />', $text[$i]);
?>
<div>
<?php print $author[$i]; ?>
<?php print $time[$i]; ?>
<?php print htmlspecialchars_decode($text[$i]); ?>
</div>
That displays and output like this:
Post 1
Post 2
Post 3
Post 4
I have tried almost anythign I found but I couldn't make it work for me. CSS column-count didnt worked also because every post has different height sizes so most posts were cut by the half at the end of one column.
I want to make the output shown like this:
Column1 Column2 Column3
Post 1 Post 2 Post 3
Post 4 Post 5 Post 6
I need to split the output in columns like that so every post can keep the different height size.
Thank you in advice for your help.
Try this (HTML borderless table-based) solution:
<table style="border: none;">
<?php
$columns = 3;
for($i=0; $i<$comments_count; $i++) {
if ($i % $columns == 0) echo "<tr>"; // begin of row
echo "<td>{$author[$i]}</td>";
echo "<td>{$time[$i]}</td>";
echo "<td>" . htmlspecialchars_decode($text[$i]) . "</td>";
if ($i % $columns == ($columns - 1)) echo "</tr>"; // end of row
} ?>
</table>
Update html code
<div class="box">
Add css
.box{float:left; width:31%; margin:3px; padding:5px;}

PHP Array Looping & Exploding

I'm not very experienced in using PHP, but I'm having a hard time thinking of a way to do this. I'm reading from a file and exploding on ":" as you can see here.
<?php
$datainfo = file('data.txt');
for($i = 0; $i < count($datainfo); $i++){
$expdata = explode(':', $datainfo[$i]);
}
?>
The issue is that I need to reference specific indexes of the resulted explosion like this.
<p> <?php echo $expdata[1] ?> </p>
I'm getting back an array of the last line inside the data.txt file. I know why It's happening, I just don't know how to get what I want here. (Sorry Very New). Data.txt contain the following.
name:Octopod Juice Stand
balance:20
price:0.5
customers:12
starting:2014-05-26
end: 2014-09-01
juice:15.25
fruit:10
Change your code to
<?php
$datainfo = file('data.txt');
$expdata = array();
for($i = 0; $i < count($datainfo); $i++){
$expdata[] = explode(':', $datainfo[$i]);
}
?>
And then to get the first label.
<p><?php echo $expdata[0][0]; ?></p>
Or the first value
<p><?php echo $expdata[0][1]; ?></p>

For Loops, and Reading Strings from a Text File, Not Working

It's hard for me to explicitly state my exact issue, but I'll try to explain the best I can.
(Sorry for the long post)
I have lines in a file called singles.txt that look like this.
Ada Lovelace,F,96,ISTJ,Linux,24,99
Adele Goldberg,F,65,ENFJ,Windows,50,70
Alan Turing,M,41,ESTP,Mac OS X,31,50
In this scenario I have a user, whos name, etc. is in this file aswell as many others. I first loop over the file to find the user's info inside the file so that I can then reference their info via variables. After this I loop over the file again, to start comparing the user's info to others found in the file. I have an if statement inside this for loop that repeats a certain div for everytime the condition is for the if statement is met. For some reason when I load the page it doesn't repeat the div at all. Hopefully my code can explain my problem more than I can.
$name = $_GET["name"];
<?php>
$singles = file('singles.txt');
for($i = 0; $i < count($singles); $i++){
$searchsingle = explode(',', $singles[$i]);
if($searchsingle[0] == $name){
$yougender = $searchsingle[1];
$youage = $searchsingle[2];
$youperson = $searchsingle[3];
$youOS = $searchsingle[4];
$youmin = $searchsingle[5];
$youmax = $searchsingle[6];
}
}
for($i = 0; $i < count($singles); $i++){
$searchsingle = explode(',', $singles[$i]);
if($yougender !== $searchsingles[1]
&& $youOS == $searchsingle[4]
&& $youmin >= $searchsingle[2]
&& $youmax <= $searchsingle[2]){
$theirname = $searchsingle[0];
$theirage = $searchsingle[2];
$theirgender = $searchsingle[1];
$theirOS = $searchsingle[4];
$theirperson = $searchsingle[3];
?>
The div to be repeated. (occurs right after the previous code)
<div class="match">
<p> <img src="user" alt="Single"> <?php echo $theirname ?> </p>
<ul>
<li><strong>Gender: <?php echo $theirgender ?> </strong></li> </br>
<li><strong>Age: <?php echo $theirage ?> </strong></li> </br>
<li><strong>Type: <?php echo $theirperson ?> </strong></li> </br>
<li><strong>OS: <?php echo $theirOS ?> </strong></li> </br>
</ul>
</p>
</div>
</body>
<?php
}
}
?>

PHP Using $i to count

Hello I want to add a number to the variable each time it loops so that I can pick up that variable later.
<?php
$i=1;
while($i<=5)
{
$myinfo.$i = "This is the text I can change";
$i++;
}
?>
<?php echo $myinfo1 ?>
<?php echo $myinfo2 ?>
<?php echo $myinfo3 ?>
<?php echo $myinfo4 ?>
<?php echo $myinfo5 ?>
I can not include the "myinfo1" in the loop because I need to add this to a table a little way down the page.
I am sorry if this is not clear but I do not know the right name for what I am trying to do.
If anyone can help that will be great.
Try this :
<?php
$i=1;
while($i<=5)
{
${'myinfo'.$i} = "This is the text I can change";
$i++;
}
?>
<?php echo $myinfo1 ?>
<?php echo $myinfo2 ?>
<?php echo $myinfo3 ?>
<?php echo $myinfo4 ?>
<?php echo $myinfo5 ?>
(but using arrays is a better solution !)
Have you considered using an array instead of named variables? By changing to this kind of architecture, you gain the ability to add more items without having to change your code (add more lines of $myInfoX). As you develop, this method will also be easier to read and add to than your current code.
For example,
$myInfo = array();
for ($i=0; $i<=5; $i++)
{
$myInfo[] = "This is the text I can change";
}
This will result in an array with a numbered index that you can recall like this:
<?php echo $myInfo[2]; //returns "This is the text I can change" ?>
You can also use the array in a loop like this:
<?php
for($info in $myInfo)
{
echo $info;
}
?>
That will print each element in the array in turn.
A for-loop would simplify things.
Try this:
$myinfo1 = "String of text 1";
$myinfo2 = "String of text 2";
$myinfo3 = "String of text 3";
$myinfo4 = "String of text 4";
$myinfo5 = "String of text 5";
for ($i=1; $i <= 5; $i++){
echo $myinfo . $i "<br>";
}
just to make you sure about this
<?php echo $myinfo1 ;?>
^-------------- you are missing this
this is should be your code like that
<?php
$myinfo1 = "This is the text I can change";
$myinfo2 = "This is the text I can change";
$myinfo3 = "This is the text I can change";
$myinfo4 = "This is the text I can change";
$myinfo5 = "This is the text I can change";
for ($i = 1; $i <= 5; $i++)
{
echo $myinfo.$i ;
}
?>

Resources