打印变量上面的while循环(范围)php

时间:2015-12-03 11:46:26

标签: php while-loop scope

是否可以在while loop下定义变量并将其打印在while loop

上方

类似这样的事情

print $catName
while ($b = mysqli_fetch_assoc($results)) {
$catName = $b['cat'];
}

修改

$getBooks = $db->prepare('SELECT
    id, cat, instituteId, title, cover, authorName FROM books_library WHERE instituteId=? AND cat=?');
$getList = array();
$getBooks->bind_param('ii', $inId, $cid);
if ($getBooks->execute()) {
    $libResults = $getBooks->get_result();
    $getList[] = $libResults;
    ?>

HTML代码来到这里

<h3 class="marginBottom8px margin-top_30px text-bold text-danger"><?php echo catName ?></h3>

然后那个循环

while ($book = mysqli_fetch_assoc($libResults)) {
                                $bokId = $book['id'];
                                $bookTitle = $bokId['title'];
                                $catName = $bokId['cat'];
                                $catName = $bokId['cat'];

现在在while loop$catName echo <h3></h3>我如何import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.ProgressDialog; import android.media.AudioManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e("Starting : ", "Starting..."); ((TextView) findViewById(R.id.status)).setText("Starting..."); // execute this when the downloader must be fired final getAppInfo downloadTask = new getAppInfo(); downloadTask.execute("http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"); } private class getAppInfo extends AsyncTask<String, String, String> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ ProgressDialog dialog; @Override protected void onPreExecute() { dialog = new ProgressDialog(MainActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setMessage("Loading"); dialog.setIndeterminate(false); dialog.setCancelable(false); dialog.setMax(100); dialog.setProgress(100); dialog.show(); } @Override protected String doInBackground(String... urls) { InputStream input = null; OutputStream output = null; HttpURLConnection connection = null; try { URL url = new URL(urls[0]); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.connect(); String length = connection.getHeaderField("Content-length"); Log.e("fileLength : ",""+length); // expect HTTP 200 OK, so we don't mistakenly save error report // instead of the file if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); } // this will be useful to display download percentage // might be -1: server did not report the length int fileLength = connection.getContentLength(); Log.e("fileLength : ",""+fileLength); // download the file input = connection.getInputStream(); // final String resultt = org.apache.commons.io.IOUtils.toString(input, "UTF-8"); // Log.e("Response : ",""+resultt); output = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/testsql.jpg"); byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { // allow canceling with back button if (isCancelled()) { input.close(); return null; } total += count; // publishing the progress.... if (fileLength > 0) // only if total length is known publishProgress(""+(int) (total * 100 / fileLength)); output.write(data, 0, count); } } catch (Exception e) { Log.e("exception : ",""+e.getMessage()); return e.toString(); } finally { try { if (output != null) output.close(); if (input != null) input.close(); } catch (IOException ignored) { } if (connection != null) connection.disconnect(); } return null; } @Override protected void onProgressUpdate(String... progress) { Log.v("count",progress[0]); dialog.setProgress(Integer.parseInt(progress[0])); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ @Override protected void onPostExecute(String result) { //dialog.setProgress(100); if(dialog!=null) dialog.dismiss(); } } } for i in $( ...

2 个答案:

答案 0 :(得分:0)

在while块之后,将h3 html放入变量并打印出来。

$myHtml = '<h3 class="marginBottom8px margin-top_30px text-bold text-danger">' 
    . $catName . '</h3>';
print $myHtml;

不要像那样混合html和php。

答案 1 :(得分:-2)

我认为这是基于编辑过的问题

之后的问题
while ($book = mysqli_fetch_assoc($libResults)) {
    echo '<h3>' . $book['cat'] . '</h3>';
}